import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/service/insurance_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/insurance_card_View_model.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/pages/insurance/insurance_details.dart';
import 'package:diplomaticquarterapp/pages/insurance/insurance_update_screen.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class InsuranceCard extends StatefulWidget {
  int appointmentNo;

  InsuranceCard({this.appointmentNo});

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

class _InsuranceCardState extends State<InsuranceCard> {
  InsuranceCardService _insuranceCardService = locator<InsuranceCardService>();
  List<ImagesInfo> imagesInfo = List();

  @override
  Widget build(BuildContext context) {
    imagesInfo.add(ImagesInfo(
        imageEn: 'https://hmgwebservices.com/Images/MobileApp/imges-info/insurance-card/en/0.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/imges-info/insurance-card/ar/0.png'));

    return BaseView<InsuranceViewModel>(
      onModelReady: (model) => model.getInsurance(),
      builder: (BuildContext context, InsuranceViewModel model, Widget child) => AppScaffold(
        isShowAppBar: true,
        baseViewModel: model,
        showHomeAppBarIcon: false,
        appBarTitle: TranslationBase.of(context).insuranceCards,
        description: TranslationBase.of(context).infoInsuranceCards,
        infoList: TranslationBase.of(context).infoInsuranceCardsPoints,
        imagesInfo: imagesInfo,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        backgroundColor: CustomColors.appBackgroudGreyColor,
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.push(context, FadePage(page: InsuranceUpdate()));
          },
          backgroundColor: CustomColors.accentColor,
          label: Text(TranslationBase.of(context).update, style: TextStyle(
            fontSize: 12.0,
            fontWeight: FontWeight.bold
          )),
        ),
        body: Container(
          child: ListView.separated(
            itemCount: model.insurance == null ? 0 : model.insurance.length,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                width: double.infinity,
                padding: EdgeInsets.only(
                  left: 10,
                  right: 10,
                  top: index == 0 ? 10 : 0,
                  bottom: index == model.insurance.length - 1 ? 100 : 0,
                ),
                child: Container(
                  decoration: cardRadius(12),
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          model.insurance[index].groupName,
                          style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, letterSpacing: -0.64),
                        ),
                        mHeight(8),
                        Text(
                          TranslationBase.of(context).companyName + model.insurance[index].companyName,
                          style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, letterSpacing: -0.48),
                        ),
                        Divider(
                          thickness: 2,
                          color: Colors.black,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  TranslationBase.of(context).category,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                                Text(
                                  model.insurance[index].subCategoryDesc,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  TranslationBase.of(context).expirationDate,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                                Text(
                                  DateUtil.getDayMonthYearDateFormatted(DateUtil.convertStringToDate(model.insurance[index].cardValidTo)),
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  TranslationBase.of(context).status + ": ",
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                                model.insurance[index].isActive == true
                                    ? Text(
                                        TranslationBase.of(context).activeInsurence,
                                        style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Colors.green),
                                      )
                                    : Text(
                                        TranslationBase.of(context).notActive,
                                        style: TextStyle(
                                          fontSize: 10,
                                          fontWeight: FontWeight.w600,
                                          color: Colors.red,
                                        ),
                                      )
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  TranslationBase.of(context).patientCard,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                                Text(
                                  model.insurance[index].patientCardID,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  TranslationBase.of(context).policyNumber,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                ),
                                Text(
                                  model.insurance[index].insurancePolicyNumber,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xFF575757)),
                                )
                              ],
                            ),
                          ],
                        ),
                        if (model.insurance[index].isActive == true)
                          SizedBox(
                            height: 8.5,
                          ),
                        if (model.insurance[index].isActive == true)
                          Container(
                            color: Colors.transparent,
                            child: SecondaryButton(
                              onTap: () => {
                                getDetails(model.insurance[index]),
                              },
                              label: TranslationBase.of(context).seeDetails,
                              textColor: Colors.white,
                            ),
                            width: double.infinity,
                          ),
                      ],
                    ),
                  ),
                ),
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return mHeight(8);
            },
          ),
        ),
      ),
    );
  }

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

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

    var date = new DateTime.fromMillisecondsSinceEpoch(int.parse(exDate.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();
  }

  getDetails(data) {
    GifLoaderDialogUtils.showMyDialog(context);
    _insuranceCardService
        .getInsuranceDetails(data)
        .then((value) => {GifLoaderDialogUtils.hideDialog(context), Navigator.push(context, FadePage(page: InsuranceCardDetails(data: value[0]['CheckList'])))});
  }
}
