import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/patient_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/PostEpisodeReqModel.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/profile_screen/profile_gird_for_InPatient.dart';
import 'package:doctor_app_flutter/screens/patients/profile/profile_screen/profile_gird_for_other.dart';
import 'package:doctor_app_flutter/screens/patients/profile/profile_screen/profile_gird_for_search.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/patient-profile-header-new-design-app-bar.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/text_fields_utils.dart';
import 'package:flutter/material.dart';

import '../../../../routes.dart';

class PatientProfileScreen extends StatefulWidget {
  @override
  _PatientProfileScreenState createState() => _PatientProfileScreenState();
}

class _PatientProfileScreenState extends State<PatientProfileScreen>
    with SingleTickerProviderStateMixin {
  PatiantInformtion patient;

  bool isFromSearch = false;

  bool isInpatient = false;

  bool isDischargedPatient = false;
  bool isSearchAndOut = false;
  String patientType;
  String arrivalType;
  String from;
  String to;
  TabController _tabController;
  int index = 0;
  int _activeTab = 0;
  @override
  void initState() {
    _tabController = TabController(length: 2, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    patient = routeArgs['patient'];
    patientType = routeArgs['patientType'];
    arrivalType = routeArgs['arrivalType'];
    from = routeArgs['from'];
    to = routeArgs['to'];
    if (routeArgs.containsKey("isSearch")) {
      isFromSearch = routeArgs['isSearch'];
    }
    if (routeArgs.containsKey("isInpatient")) {
      isInpatient = routeArgs['isInpatient'];
    }
    if (routeArgs.containsKey("isDischargedPatient")) {
      isDischargedPatient = routeArgs['isDischargedPatient'];
    }
    if (routeArgs.containsKey("isSearchAndOut")) {
      isSearchAndOut = routeArgs['isSearchAndOut'];
    }
    if (isInpatient)
      _activeTab = 0;
    else
      _activeTab = 1;
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return BaseView<PatientViewModel>(
        builder: (_, patientViewModel, w) => AppScaffold(
              baseViewModel: patientViewModel,
              appBarTitle: TranslationBase.of(context).patientProfile,
              isShowAppBar: false,
              body: Column(
                children: [
                  Stack(
                    children: [
                      Column(
                        children: [
                          PatientProfileHeaderNewDesignAppBar(

                            patient,
                            arrivalType ?? '0',
                            patientType,
                            isInpatient: isInpatient,
                            height: (patient.patientStatusType != null &&
                                    patient.patientStatusType == 43)
                                ? 210
                                : isDischargedPatient
                                    ? 240
                                    : 0,
                              isDischargedPatient:isDischargedPatient
                          ),
                          Container(
                            height: !isSearchAndOut
                                ? isDischargedPatient
                                    ? MediaQuery.of(context).size.height * 0.64
                                    : MediaQuery.of(context).size.height * 0.65
                                : MediaQuery.of(context).size.height * 0.69,
                            child: ListView(
                              children: [
                                Container(
                                  child: isSearchAndOut
                                      ? ProfileGridForSearch(
                                          patient: patient,
                                          patientType: patientType,
                                          arrivalType: arrivalType,
                                          isInpatient: isInpatient,
                                          from: from,
                                          to: to,
                                        )
                                      : isInpatient
                                          ? ProfileGridForInPatient(
                                              patient: patient,
                                              patientType: patientType,
                                              arrivalType: arrivalType,
                                              isInpatient: isInpatient,
                                              from: from,
                                              to: to,
                                              isDischargedPatient:
                                                  isDischargedPatient,
                                              isFromSearch: isFromSearch,
                                            )
                                          : ProfileGridForOther(
                                              patient: patient,
                                              patientType: patientType,
                                              arrivalType: arrivalType,
                                              isInpatient: isInpatient,
                                              from: from,
                                              to: to,
                                            ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      if (patient.patientStatusType != null &&
                          patient.patientStatusType == 43)
                        BaseView<SOAPViewModel>(
                            onModelReady: (model) async {},
                            builder: (_, model, w) => Positioned(
                                  top: 180,
                                  left: 20,
                                  right: 20,
                                  child: Row(
                                    children: [
                                      Expanded(child: Container()),
                                      if (patient.episodeNo == 0)
                                        AppButton(
                                          title:
                                              "${TranslationBase.of(context).createNew}\n${TranslationBase.of(context).episode}",
                                          color: patient.patientStatusType == 43
                                              ? Colors.red.shade700
                                              : Colors.grey.shade700,
                                          fontColor: Colors.white,
                                          vPadding: 8,
                                          radius: 30,
                                          hPadding: 20,
                                          fontWeight: FontWeight.normal,
                                          fontSize: 1.6,
                                          icon: Image.asset(
                                            "assets/images/create-episod.png",
                                            color: Colors.white,
                                            height: 30,
                                          ),
                                          onPressed: () async {
                                            if (patient.patientStatusType ==
                                                43) {
                                              PostEpisodeReqModel
                                                  postEpisodeReqModel =
                                                  PostEpisodeReqModel(
                                                      appointmentNo:
                                                          patient.appointmentNo,
                                                      patientMRN:
                                                          patient.patientMRN);
                                              GifLoaderDialogUtils.showMyDialog(
                                                  context);
                                              await model.postEpisode(
                                                  postEpisodeReqModel);
                                              GifLoaderDialogUtils.hideDialog(
                                                  context);
                                              patient.episodeNo =
                                                  model.episodeID;
                                              Navigator.of(context).pushNamed(
                                                  CREATE_EPISODE,
                                                  arguments: {
                                                    'patient': patient
                                                  });
                                            }
                                          },
                                        ),
                                      if (patient.episodeNo != 0)
                                        AppButton(
                                            title:
                                                "${TranslationBase.of(context).update}\n${TranslationBase.of(context).episode}",
                                            color:
                                                patient.patientStatusType == 43
                                                    ? Colors.red.shade700
                                                    : Colors.grey.shade700,
                                            fontColor: Colors.white,
                                            vPadding: 8,
                                            radius: 30,
                                            hPadding: 20,
                                            fontWeight: FontWeight.normal,
                                            fontSize: 1.6,
                                            icon: Image.asset(
                                              "assets/images/modilfy-episode.png",
                                              color: Colors.white,
                                              height: 30,
                                            ),
                                            onPressed: () {
                                              if (patient.patientStatusType ==
                                                  43) {
                                                Navigator.of(context).pushNamed(
                                                    UPDATE_EPISODE,
                                                    arguments: {
                                                      'patient': patient
                                                    });
                                              }
                                            }),
                                    ],
                                  ),
                                )),
                    ],
                  ),
                ],
              ),
            ));
  }

  Widget tabsBar(BuildContext context, Size screenSize) {
    List<String> _tabs = [
      "Inpatient Info".toUpperCase(),
      "Outpatient Info".toUpperCase(),
    ];

    return Container(
      height: screenSize.height * 0.070,
      decoration: TextFieldsUtils.containerBorderDecoration(
          Color(0Xffffffff), Color(0xFFCCCCCC),
          borderRadius: 4, borderWidth: 0),
      child: Row(
        mainAxisSize: MainAxisSize.max,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: _tabs.map((item) {
          bool _isActive = _tabs[_activeTab] == item ? true : false;

          return Expanded(
            child: InkWell(
              onTap: () async {
                setState(() {
                  _activeTab = _tabs.indexOf(item);
                });
              },
              child: Center(
                child: Container(
                  height: screenSize.height * 0.070,
                  decoration: TextFieldsUtils.containerBorderDecoration(
                      _isActive
                          ? Color(0xFFD02127 /*B8382B*/)
                          : Color(0xFFEAEAEA),
                      _isActive ? Color(0xFFD02127) : Color(0xFFEAEAEA),
                      borderRadius: 4,
                      borderWidth: 0),
                  child: Center(
                    child: AppText(
                      item,
                      fontSize: SizeConfig.textMultiplier * 1.8,
                      color: _isActive ? Colors.white : Color(0xFF2B353E),
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }
}

class AvatarWidget extends StatelessWidget {
  final Widget avatarIcon;

  AvatarWidget(this.avatarIcon);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
              color: Color.fromRGBO(0, 0, 0, 0.08),
              offset: Offset(0.0, 5.0),
              blurRadius: 16.0)
        ],
        borderRadius: BorderRadius.all(Radius.circular(35.0)),
        color: Color(0xffCCCCCC),
      ),
      child: avatarIcon,
    );
  }
}
