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/uitl/utils_new.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: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Column(
          children: [
            Container(
              width: double.infinity,
              child: Container(
                decoration: cardRadius(12),
                child: Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: InkWell(
                    onTap: () {
                      getDetails(
                        setupID: '010266',
                        projectID: 15,
                        patientIdentificationID: model.user.patientIdentificationNo,
                        patientID: model.user.patientID,
                        name: model.user.firstName + " " + model.user.lastName,
                        context: context,
                      );
                    },
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      mainAxisSize: MainAxisSize.max,
                      children: <Widget>[
                        if (model.user != null)
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                  model.user.firstName + " " + model.user.lastName,
                                  style: TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.w600,
                                    letterSpacing: -0.46,
                                  ),
                                ),
                                Text(
                                  TranslationBase.of(context).fileno + ": " + model.user.patientID.toString(),
                                  style: TextStyle(
                                    fontSize: 12,
                                    fontWeight: FontWeight.w600,
                                    letterSpacing: -0.46,
                                  ),
                                )
                              ],
                            ),
                          ),
                        Icon(Icons.arrow_forward),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            if (model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList != null ?? false)
              ListView.separated(
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList[index].status == 3
                      ? InkWell(
                          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);
                          },
                          child: Container(
                            width: double.infinity,
                            margin: EdgeInsets.only(top: 12.0),
                            child: Container(
                              decoration: cardRadius(12),
                              child: Padding(
                                padding: const EdgeInsets.all(12.0),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  mainAxisSize: MainAxisSize.max,
                                  children: [
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList[index].patientName,
                                            style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, letterSpacing: -0.46),
                                          ),
                                          Text(
                                            TranslationBase.of(context).fileno + ": " + model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList[index].responseID.toString(),
                                            style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, letterSpacing: -0.46),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Icon(Icons.arrow_forward),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        )
                      : Container();
                },
                separatorBuilder: (context, index) {
                  return mHeight(model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList[index].status == 3 ? 8 : 0);
                },
                itemCount: model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList.length,
              ),
          ],
        ),
      ),
    );
  }

  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,
            ))).then((value) {
          model.getInsuranceUpdated();
        });
      } else {
        AppToast.showErrorToast(message: _insuranceCardService.error);
      }
    });
  }
}
