import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_templateModel.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_template_details_model.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ExpansionProcedure extends StatefulWidget {
  final ProcedureTempleteDetailsModelList? procedureTempleteModel;
  final ProcedureViewModel? model;
  final Function(ProcedureTempleteDetailsModel)? removeFavProcedure;
  final Function(ProcedureTempleteDetailsModel)? addFavProcedure;
  final Function(ProcedureTempleteDetailsModel) ?selectProcedures;
  final bool Function(ProcedureTempleteModel)? isEntityListSelected;
  final bool Function(ProcedureTempleteDetailsModel)? isEntityFavListSelected;
  final bool isProcedure;
  final ProcedureTempleteDetailsModel? groupProcedures;

  const ExpansionProcedure(
      {Key? key,
      this.procedureTempleteModel,
      this.model,
      this.removeFavProcedure,
      this.addFavProcedure,
      this.selectProcedures,
      this.isEntityListSelected,
      this.isEntityFavListSelected,
      this.isProcedure = true,
      this.groupProcedures})
      : super(key: key);

  @override
  _ExpansionProcedureState createState() => _ExpansionProcedureState();
}

class _ExpansionProcedureState extends State<ExpansionProcedure> {
  bool _isShowMore = false;
  BaseAppClient baseAppClient = BaseAppClient();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        InkWell(
          onTap: () async {
            setState(() {
              _isShowMore = !_isShowMore;
            });
          },
          child: Container(
            padding: EdgeInsets.all(10.0),
            margin: EdgeInsets.only(left: 5, right: 5),
            decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                color: Colors.white,
                borderRadius: BorderRadius.all(
                  Radius.circular(5.0),
                )),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                    child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Icon(
                      Icons.folder,
                      size: 20,
                      color: AppGlobal.appTextColor,
                    ),
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 10, vertical: 0),
                        child: AppText(
                          widget.isProcedure == true
                              ? "Procedures for " +
                                  widget.procedureTempleteModel!.templateName!
                              : "Prescription for " +
                                  widget.procedureTempleteModel!.templateName!,
                          letterSpacing: -0.72,
                          fontSize: 16.0,
                          color: AppGlobal.appTextColor,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ],
                )),
                Container(
                  width: 25,
                  height: 25,
                  child: Icon(
                    _isShowMore
                        ? Icons.keyboard_arrow_up
                        : Icons.keyboard_arrow_down,
                    color: AppGlobal.appTextColor,
                    size: 28,
                  ),
                )
              ],
            ),
          ),
        ),
        if (_isShowMore)
          AnimatedContainer(
            padding: EdgeInsets.all(10.0),
            margin: EdgeInsets.only(left: 5, right: 5),
            decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(5.0),
                  bottomRight: Radius.circular(5.0),
                )),
            duration: Duration(milliseconds: 7000),
            child: Column(
              children: widget.procedureTempleteModel!.procedureTemplate
                  .map((itemProcedure) {
                return InkWell(
                  onTap: () {
                    if (widget.isProcedure) {
                      setState(() {
                        if (widget.isEntityFavListSelected!(itemProcedure)) {
                          widget.removeFavProcedure!(itemProcedure);
                        } else {
                          widget.addFavProcedure!(itemProcedure);
                        }
                      });
                    } else {
                      widget.selectProcedures!(itemProcedure);
                    }
                  },
                  child: Container(
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 12),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 11),
                                  child: widget.isProcedure
                                      ? Checkbox(
                                          value: widget.isEntityFavListSelected!(
                                              itemProcedure),
                                          activeColor: Color(0xffD02127),
                                          onChanged: (bool? newValue) {
                                            setState(() {
                                              if (widget
                                                  .isEntityFavListSelected!(
                                                      itemProcedure)) {
                                                widget.removeFavProcedure!(
                                                    itemProcedure);
                                              } else {
                                                widget.addFavProcedure!(
                                                    itemProcedure);
                                              }
                                            });
                                          })
                                      : Radio(
                                          value: itemProcedure,
                                          groupValue: widget.groupProcedures,
                                          activeColor: Color(0xffD02127),
                                          onChanged: (newValue) {
                                            widget.selectProcedures!(newValue!);
                                          })),
                              Expanded(
                                child: Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 10, vertical: 0),
                                  child: AppText(
                                    Utils.convertToTitleCase(
                                        itemProcedure.procedureName!),
                                    fontSize: 14.0,
                                    variant: "bodyText",
                                    bold: true,
                                    color: Color(0xff575757),
                                    fontFamily: 'Poppins-Bold.ttf',
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }).toList(),
            ),
          ),
        SizedBox(
          height: 10,
        ),
      ],
    );
  }
}
