import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/viewModels/insurance_card_View_model.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/rounded_container.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../base/base_view.dart';

class InsuranceApproval extends StatefulWidget {
  int appointmentNo;

  InsuranceApproval({this.appointmentNo});

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

class _InsuranceApprovalState extends State<InsuranceApproval> {
  List<ImagesInfo> imagesInfo = List();

  @override
  Widget build(BuildContext context) {
    imagesInfo
        .add(ImagesInfo(imageEn: 'https://hmgwebservices.com/Images/MobileApp/imges-info/apporvals/en/0.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/imges-info/apporvals/ar/0.png'));
    imagesInfo
        .add(ImagesInfo(imageEn: 'https://hmgwebservices.com/Images/MobileApp/imges-info/apporvals/en/1.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/imges-info/apporvals/ar/1.png'));
    return BaseView<InsuranceViewModel>(
      onModelReady: widget.appointmentNo != null ? (model) => model.getInsuranceApproval(appointmentNo: widget.appointmentNo) : (model) => model.getInsuranceApproval(),
      builder: (BuildContext context, InsuranceViewModel model, Widget child) => AppScaffold(
        isShowAppBar: true,
        baseViewModel: model,
        appBarTitle: TranslationBase.of(context).approvals,
        description: TranslationBase.of(context).infoApprovals,
        infoList: TranslationBase.of(context).infoApprovalPoints,
        imagesInfo: imagesInfo,
        body: SingleChildScrollView(
          child: Container(
            margin: EdgeInsets.only(
              left: SizeConfig.screenWidth * 0.004,
              right: SizeConfig.screenWidth * 0.004,
              top: SizeConfig.screenWidth * 0.04,
            ),
            child: Column(
              children: <Widget>[
                Container(
                    width: double.infinity,
                    height: SizeConfig.screenHeight * 0.09,
                    color: Color(0xffEEEEEE),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Texts(
                          TranslationBase.of(context).totalApproval,
                          color: Color(0xff60688B),
                          fontSize: 19.0,
                          fontWeight: FontWeight.w600,
                        ),
                        if (model.insuranceApproval.length > 0)
                          Container(
                              width: 60,
                              height: 40,
                              decoration: BoxDecoration(color: Theme.of(context).primaryColor, borderRadius: BorderRadius.circular(19.0)),
                              child: Center(
                                child: Texts(
                                  model.insuranceApproval[0].unUsedCount.toString(),
                                  color: Colors.white,
                                  fontSize: 17.0,
                                ),
                              ))
                      ],
                    )),
                ...List.generate(model.insuranceApproval.length, (index) {
                  print(model.insuranceApproval[index].isLiveCareAppointment);
                  return RoundedContainer(
                    backgroundColor: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        ExpansionTile(
                          title: Container(
                            //height: 120.0,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                model.insuranceApproval[index].isLiveCareAppointment
                                    ? sideBadge(TranslationBase.of(context).liveCare, 0xffA9A089)
                                    : model.insuranceApproval[index].isInOutPatient
                                        ? sideBadge(TranslationBase.of(context).outpatient, 0xff505a5d)
                                        : sideBadge(TranslationBase.of(context).inPatient, 0xffb8372c),
                                Padding(
                                  padding: EdgeInsets.symmetric(vertical: 10.0),
                                  child: Texts(
                                    model.insuranceApproval[index].clinicName,
                                    fontSize: 20.0,
                                    color: Color(0xff60686B),
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                                Texts(
                                  model.insuranceApproval[index].doctorName,
                                  fontSize: 17.0,
                                ),
                              ],
                            ),
                          ),
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.symmetric(vertical: 12.0, horizontal: 12.0),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Row(
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).approvalNo,
                                        fontSize: 18.0,
                                      ),
                                      Texts(
                                        model.insuranceApproval[index].approvalNo.toString(),
                                        fontSize: 18.0,
                                        fontWeight: FontWeight.w600,
                                      ),
                                    ],
                                  ),
                                  Divider(
                                    color: Colors.black,
                                    height: 25.0,
                                    thickness: 1.0,
                                  ),
                                  Row(
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).procedureStatus,
                                        fontSize: 17.5,
                                      ),
                                      SizedBox(
                                        width: 12,
                                      ),
                                      Texts(
                                        model.insuranceApproval[index].approvalStatusDescption,
                                        fontWeight: FontWeight.w600,
                                        fontSize: 17.5,
                                      ),
                                    ],
                                  ),
                                  Divider(
                                    color: Colors.black,
                                    height: 25.0,
                                    thickness: 1.0,
                                  ),
                                  Row(
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).unusedCount,
                                        fontSize: 17.5,
                                      ),
                                      Texts(
                                        model.insuranceApproval[index].unUsedCount.toString(),
                                        fontSize: 17.5,
                                        fontWeight: FontWeight.w600,
                                      ),
                                    ],
                                  ),
                                  Divider(
                                    color: Colors.black,
                                    height: 25.0,
                                    thickness: 1.0,
                                  ),
//                                Text(
//                                  'Company Name:    ' +
//                                              model.insuranceApproval[index]
//                                                  .companyName ==
//                                          null
//                                      ? '000'
//                                      : model
//                                          .insuranceApproval[index].companyName,
//                                  style: TextStyle(
//                                      fontSize: 17.5,
//                                      fontWeight: FontWeight.w600),
//                                ),
                                  Texts(
                                    TranslationBase.of(context).companyName,
                                    fontWeight: FontWeight.w600,
                                    fontSize: 17.5,
                                  ),

                                  Divider(
                                    color: Colors.black,
                                    height: 25.0,
                                    thickness: 1.0,
                                  ),
                                  Row(
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).receiptOn,
                                        fontSize: 17.5,
                                        fontWeight: FontWeight.w600,
                                      ),
                                      Texts(
                                        convertDateFormat(model.insuranceApproval[index].receiptOn),
                                        fontSize: 17.5,
                                        fontWeight: FontWeight.w600,
                                      ),
                                    ],
                                  ),
                                  Divider(
                                    color: Colors.black,
                                    height: 25.0,
                                    thickness: 1.0,
                                  ),
                                  Row(
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).expiryDate,
                                        fontSize: 17.5,
                                        fontWeight: FontWeight.w600,
                                      ),
                                      Texts(
                                        convertDateFormat(model.insuranceApproval[index].expiryDate),
                                        fontSize: 17.5,
                                        fontWeight: FontWeight.w600,
                                      ),
                                    ],
                                  ),
                                  Divider(
                                    color: Colors.black,
                                    height: 55.0,
                                    thickness: 1.2,
                                  ),
                                  IntrinsicHeight(
                                    child: Row(
                                      crossAxisAlignment: CrossAxisAlignment.center,
                                      children: <Widget>[
                                        Expanded(
                                          flex: 1,
                                          child: Column(
                                            children: <Widget>[
                                              Text(TranslationBase.of(context).procedureName),
                                              Padding(
                                                padding: EdgeInsets.only(top: SizeConfig.heightMultiplier * 3.0),
                                                child: Text(
                                                  model.insuranceApproval[index].apporvalDetails?.procedureName ?? '',
                                                  style: TextStyle(fontWeight: FontWeight.w600, fontSize: 15.5),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        VerticalDivider(
                                          width: 10.0,
                                          thickness: 1.2,
                                          color: Colors.black,
                                        ),
                                        Expanded(
                                          flex: 1,
                                          child: Column(
                                            children: <Widget>[
                                              Text(TranslationBase.of(context).procedureStatus),
                                              Padding(
                                                padding: EdgeInsets.only(top: SizeConfig.heightMultiplier * 3.0),
                                                child: Text(
                                                  model.insuranceApproval[index].apporvalDetails?.status ?? '',
                                                  style: TextStyle(fontSize: 17.5, fontWeight: FontWeight.w600),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        VerticalDivider(
                                          width: 2.3,
                                          thickness: 1.2,
                                          color: Colors.black,
                                        ),
                                        Expanded(
                                          flex: 1,
                                          child: Column(
                                            children: <Widget>[
                                              Text(TranslationBase.of(context).usageStatus),
                                              Padding(
                                                padding: EdgeInsets.only(top: SizeConfig.heightMultiplier * 3.0),
                                                child: Text(
                                                  model.insuranceApproval[index].apporvalDetails?.isInvoicedDesc ?? '',
                                                  style: TextStyle(fontWeight: FontWeight.w600, fontSize: 17.5),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Divider(
                                    color: Colors.black,
                                    height: 25.0,
                                    thickness: 1.0,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  );
                }),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget sideBadge(String title, int color) {
    return Container(
      decoration: BoxDecoration(
        color: Color(color),
        borderRadius: BorderRadius.circular(16.0),
      ),
      width: 115.0,
      padding: EdgeInsets.only(top: 1, bottom: 1),
      alignment: Alignment.center,
      child: Texts(
        title,
        color: Colors.white,
      ),
    );
  }

  convertDateFormat(String Date) {
    const start = "/Date(";
    const end = "+0300)";

    final startIndex = Date.indexOf(start);
    final endIndex = Date.indexOf(end, startIndex + start.length);

    var date = new DateTime.fromMillisecondsSinceEpoch(int.parse(Date.substring(startIndex + start.length, endIndex)));
    String newDate = date.year.toString() + "-" + date.month.toString().padLeft(2, '0') + "-" + date.day.toString().padLeft(2, '0');

    return newDate.toString();
  }
}
