import 'package:hmg_patient_app/core/model/ImagesInfo.dart';
import 'package:hmg_patient_app/core/viewModels/insurance_card_View_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/extensions/string_extensions.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/avatar/large_avatar.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/my_rich_text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

class InsuranceApproval extends StatefulWidget {
  int? appointmentNo;

  InsuranceApproval({ this.appointmentNo});

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

class _InsuranceApprovalState extends State<InsuranceApproval> {
  List<ImagesInfo> imagesInfo =[];

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(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,
          showNewAppBar: true,
          baseViewModel: model,
          showNewAppBarTitle: true,
          appBarTitle: TranslationBase.of(context).insuranceApprovalSubtitle,
          description: TranslationBase.of(context).infoApprovals,
          infoList: TranslationBase.of(context).infoApprovalPoints,
          imagesInfo: imagesInfo,
          body: model.insuranceApproval.length > 0
              ? ListView.separated(
                  padding: EdgeInsets.all(21),
                  physics: BouncingScrollPhysics(),
                  itemBuilder: (_, index) {
                    Color _patientStatusColor;
                    String _patientStatusString;

                    if (model.insuranceApproval[index].isLiveCareAppointment!) {
                      _patientStatusColor = Color(0xff2E303A);
                      _patientStatusString = TranslationBase.of(context).liveCare.capitalizeFirstofEach;
                    } else if (!model.insuranceApproval[index].isInOutPatient!) {
                      _patientStatusColor = Color(0xffD02127);
                      _patientStatusString = TranslationBase.of(context).inPatient.capitalizeFirstofEach;
                    } else {
                      _patientStatusString = TranslationBase.of(context).outpatient.capitalizeFirstofEach;
                      _patientStatusColor = Color(0xffa9a089);
                    }
                    return InkWell(
                      onTap: () {
                        Navigator.push(context, FadePage(page: InsuranceApprovalDetail(insuranceApprovalModel: model.insuranceApproval[index])));
                      },
                      child: Container(
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(
                              Radius.circular(10.0),
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: Color(0xff000000).withOpacity(.05),
                                //spreadRadius: 5,
                                blurRadius: 27,
                                offset: Offset(0, -3),
                              ),
                            ],
                            color: _patientStatusColor),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 16,
                              child: RotatedBox(
                                quarterTurns: 3,
                                child: Text(
                                  _patientStatusString,
                                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w700, color: Colors.white, letterSpacing: -0.2, height: 16 / 10),
                                ),
                              ),
                            ),
                            Expanded(
                              child: Container(
                                padding: EdgeInsets.fromLTRB(12, 10, 12, 10),
                                decoration: BoxDecoration(
                                    border: Border.all(color: Colors.white, width: 1),
                                    borderRadius: BorderRadius.only(
                                      bottomRight: Radius.circular(10.0),
                                      topRight: Radius.circular(10.0),
                                    ),
                                    color: Colors.white),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      model.insuranceApproval[index].approvalStatusDescption!,
                                      style: TextStyle(
                                          fontSize: 12,
                                          fontWeight: FontWeight.w600,
                                          color: model.insuranceApproval[index].status == 9 ? Color(0xff359846) : Color(0xffD02127),
                                          letterSpacing: -0.4,
                                          height: 18 / 10),
                                    ),
                                    Text(
                                      model.insuranceApproval[index].doctorName!,
                                      style: TextStyle(
                                        fontSize: 16,
                                        fontWeight: FontWeight.w600,
                                        color: Color(0xff2E303A),
                                        letterSpacing: -0.64,
                                      ),
                                    ),
                                    SizedBox(height: 6),
                                    Row(
                                      children: [
                                        LargeAvatar(
                                          name: model.insuranceApproval[index].doctorName!,
                                          url: model.insuranceApproval[index].doctorImageURL!,
                                          width: 48,
                                          height: 48,
                                        ),
                                        SizedBox(width: 12),
                                        Expanded(
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            children: [
                                              MyRichText(TranslationBase.of(context).clinic + ":", model.insuranceApproval[index].clinicName != null ? model.insuranceApproval[index].clinicName!.toLowerCase().capitalizeFirstofEach : "",
                                                  projectViewModel.isArabic),
                                              MyRichText(TranslationBase.of(context).approvalNo, model.insuranceApproval[index].approvalNo.toString() ?? "", projectViewModel.isArabic),
                                            ],
                                          ),
                                        ),
                                        Container(
                                          height: 48,
                                          alignment: Alignment.bottomRight,
                                          child: Icon(
                                            Icons.arrow_forward,
                                            color: Color(0xff2E303A),
                                          ),
                                        )
                                      ],
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (context, index) => SizedBox(height: 12),
                  itemCount: model.insuranceApproval.length)
              : getNoDataWidget(context)),
    );
  }

  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();
  }
}
