import 'package:auto_size_text/auto_size_text.dart';
import 'package:hmg_patient_app/config/size_config.dart';
import 'package:hmg_patient_app/models/Clinics/ClinicListResponse.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_svg/svg.dart';

showClickListDialog(BuildContext context, List<ListClinicCentralized> clinicsList, {Function(ListClinicCentralized)? onSelection}) {
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(4.0),
        ), //this right here
        child: Container(
          width: double.infinity,
          // width: MediaQuery.of(context).size.width - 10,
          height: MediaQuery.of(context).size.height - 80,
          child: ClickListDialog(
            clinicsList: clinicsList,
            onSelection: onSelection!,
          ),
        ),
      );
    },
  );
}

class ClickListDialog extends StatefulWidget {
  List<ListClinicCentralized>? clinicsList;
  Function(ListClinicCentralized)? onSelection;

  ClickListDialog({this.clinicsList, this.onSelection});

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

class _ClickListDialogState extends State<ClickListDialog> {
  TextEditingController controller = new TextEditingController();
  List<ListClinicCentralized> tempClinicsList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    addAllData();
  }

  addAllData() {
    tempClinicsList.clear();
    for (int i = 0; i < widget.clinicsList!.length; i++) {
      tempClinicsList.add(widget.clinicsList![i]);
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              controller: controller,
              onChanged: (v) {
                if (v.length > 0) {
                  tempClinicsList.clear();
                  for (int i = 0; i < widget.clinicsList!.length; i++) {
                    if (widget.clinicsList![i].clinicDescription!.toLowerCase().contains(v.toLowerCase())) {
                      tempClinicsList.add(widget.clinicsList![i]);
                    }
                  }
                } else {
                  addAllData();
                }
                setState(() {});
              },
              decoration: InputDecoration(
                hintStyle: TextStyle(fontSize: 17),
                hintText: TranslationBase.of(context).searchClinic,
                suffixIcon: Icon(Icons.search),
                border: InputBorder.none,
                contentPadding: EdgeInsets.all(12),
              ),
            ),
          ),
          Expanded(
            child: ListView.separated(
              padding: EdgeInsets.all(12),
              itemBuilder: (context, index) {
                return InkWell(
                  onTap: () {
                    widget.onSelection!(tempClinicsList[index]);
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: AutoSizeText(
                            tempClinicsList[index].clinicDescription!.trim(),
                            maxLines: 1,
                            minFontSize: 10,
                            style: TextStyle(
                              fontSize: SizeConfig.textMultiplier! * 1.4,
                              fontWeight: FontWeight.w600,
                              letterSpacing: -0.39,
                              height: 0.8,
                            ),
                          ),
                        ),
                        tempClinicsList[index].isLiveCareClinicAndOnline == true
                            ? SvgPicture.asset(
                                'assets/images/new-design/video_icon_green_right.svg',
                                height: 12,
                                width: 12,
                                fit: BoxFit.cover,
                              )
                            : Container(),
                      ],
                    ),
                  ),
                );
              },
              itemCount: tempClinicsList.length,
              separatorBuilder: (BuildContext context, int index) {
                return mHeight(8);
              },
            ),
          ),
        ],
      ),
    );
  }
}
