import 'package:diplomaticquarterapp/core/service/insurance_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/insurance_card_View_model.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../locator.dart';
import 'insurance_card_update_details.dart';

class InsurancePage extends StatelessWidget {
  final InsuranceViewModel model;
  InsuranceCardService _insuranceCardService = locator<InsuranceCardService>();

  InsurancePage({Key key, this.model}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(
            height: 65,
          ),
          Container(
            margin: EdgeInsets.all(10.0),
            color: Colors.white,
            width: MediaQuery.of(context).size.width,
            padding: EdgeInsets.all(10.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                if (model.user != null)
                  Expanded(
                    flex: 3,
                    child: Container(
                      margin:
                          EdgeInsets.only(top: 2.0, left: 10.0, right: 20.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Texts(
                            model.user.firstName + " " + model.user.lastName,
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w500,
                          ),
                          SizedBox(
                            height: 8,
                          ),
                          Texts(
                            TranslationBase.of(context).fileno +
                                ": " +
                                model.user.patientID.toString(),
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w500,
                          )
                        ],
                      ),
                    ),
                  ),
                Expanded(
                  flex: 2,
                  child: Container(
                    margin: EdgeInsets.only(top: 2.0),
                    child: Column(
                      children: <Widget>[
                        Container(
                          child: SecondaryButton(
                            label: TranslationBase.of(context).fetchData,
                            small: true,
                            textColor: Colors.white,
                            onTap: () {
                              getDetails(
                                  setupID: '010266',
                                  projectID: 15,
                                  patientIdentificationID:
                                      model.user.patientIdentificationNo,
                                  patientID: model.user.patientID,
                                  name: model.user.firstName +
                                      " " +
                                      model.user.lastName,
                                  context: context);
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
          if (model.getAllSharedRecordsByStatusResponse
                      .getAllSharedRecordsByStatusList !=
                  null ??
              false)
            ...List.generate(
                model.getAllSharedRecordsByStatusResponse
                    .getAllSharedRecordsByStatusList.length,
                (index) => model.getAllSharedRecordsByStatusResponse
                            .getAllSharedRecordsByStatusList[index].status ==
                        3
                    ? Container(
                        margin: EdgeInsets.all(10.0),
                        child: Card(
                          margin: EdgeInsets.fromLTRB(8.0, 16.0, 8.0, 8.0),
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            padding: EdgeInsets.all(10.0),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.max,
                              children: <Widget>[
                                Expanded(
                                  flex: 3,
                                  child: Container(
                                    margin: EdgeInsets.only(
                                        top: 2.0, left: 10.0, right: 20.0),
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Texts(
                                          model
                                              .getAllSharedRecordsByStatusResponse
                                              .getAllSharedRecordsByStatusList[
                                                  index]
                                              .patientName,
                                          fontSize: 14,
                                          color: Colors.black,
                                          fontWeight: FontWeight.w500,
                                        ),
                                        SizedBox(
                                          height: 8,
                                        ),
                                        Texts(
                                          TranslationBase.of(context).fileno +
                                              ": " +
                                              model
                                                  .getAllSharedRecordsByStatusResponse
                                                  .getAllSharedRecordsByStatusList[
                                                      index]
                                                  .patientID
                                                  .toString(),
                                          fontSize: 14,
                                          color: Colors.black,
                                          fontWeight: FontWeight.w500,
                                        )
                                      ],
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 2,
                                  child: Container(
                                    margin: EdgeInsets.only(top: 2.0),
                                    child: Column(
                                      children: <Widget>[
                                        Container(
                                          child: SecondaryButton(
                                            label: TranslationBase.of(context)
                                                .fetchData,
                                            small: true,
                                            textColor: Colors.white,
                                            onTap: () {
                                              getDetails(
                                                  projectID: 15,
                                                  patientIdentificationID: model
                                                      .getAllSharedRecordsByStatusResponse
                                                      .getAllSharedRecordsByStatusList[
                                                          index]
                                                      .patientIdenficationNumber,
                                                  setupID: '010266',
                                                  patientID: model
                                                      .getAllSharedRecordsByStatusResponse
                                                      .getAllSharedRecordsByStatusList[
                                                          index]
                                                      .responseID,
                                                  name: model
                                                      .getAllSharedRecordsByStatusResponse
                                                      .getAllSharedRecordsByStatusList[
                                                          index]
                                                      .patientName,
                                                  context: context);
                                            },
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                      )
                    : Container()),
        ],
      ),
    );
  }

  getDetails(
      {String setupID,
      int projectID,
      String patientIdentificationID,
      int patientID,
      String name,
      BuildContext context}) {
    GifLoaderDialogUtils.showMyDialog(context);
    _insuranceCardService
        .getPatientInsuranceDetails(
            setupID: setupID,
            projectID: projectID,
            patientID: patientID,
            patientIdentificationID: patientIdentificationID)
        .then((value) {
      GifLoaderDialogUtils.hideDialog(context);
      if (!_insuranceCardService.hasError &&
          _insuranceCardService.isHaveInsuranceCard) {
        Navigator.push(
            context,
            FadePage(
                page: InsuranceCardUpdateDetails(
              insuranceCardDetailsModel:
                  _insuranceCardService.insuranceCardDetailsList,
              patientID: patientID,
              patientIdentificationID: patientIdentificationID,
              name: name,
            )));
      } else {
        AppToast.showErrorToast(message: _insuranceCardService.error);
      }
    });
  }
}
