import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/PatientERVirtualHistoryResponse.dart';
import 'package:diplomaticquarterapp/pages/livecare/widgets/LiveCarePendingRequest.dart';
import 'package:diplomaticquarterapp/pages/livecare/widgets/clinic_list.dart';
import 'package:diplomaticquarterapp/pages/livecare/widgets/livecare_logs.dart';
import 'package:diplomaticquarterapp/services/livecare_services/livecare_provider.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LiveCareHome extends StatefulWidget {
  static bool showFooterButton = true;
  static bool isLiveCareTypeSelected = false;

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

class _LiveCareHomeState extends State<LiveCareHome> with SingleTickerProviderStateMixin {
  TabController _tabController;

  bool isDataLoaded = false;
  bool hasLiveCareRequest = false;
  List<ImagesInfo> imagesInfo = List();

  List<ErRequestHistoryList> erRequestHistoryList;

  ErRequestHistoryList pendingERRequestHistoryList;

  ProjectViewModel projectViewModel;

  @override
  void initState() {
    _tabController = new TabController(length: 2, vsync: this);
    erRequestHistoryList = List();

    pendingERRequestHistoryList = new ErRequestHistoryList();
    imagesInfo.add(ImagesInfo(
        imageEn: 'https://hmgwebservices.com/Images/MobileApp/imges-info/er-consultation_en/en/0.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/imges-info/er-consultation_ar/ar/0.png'));
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (!isDataLoaded && projectViewModel.isLogin) getLiveCareHistory();
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).livecare,
      isShowAppBar: true,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      imagesInfo: imagesInfo,
      description: TranslationBase.of(context).erConsultation,
      body: Container(
        child: Column(children: [
          TabBar(
            controller: _tabController,
            indicatorWeight: 3.0,
            indicatorSize: TabBarIndicatorSize.tab,
            labelColor: Color(0xff2B353E),
            unselectedLabelColor: Color(0xff575757),
            labelPadding: EdgeInsets.only(top: 0, bottom: 0, left: 20, right: 20),
            labelStyle: TextStyle(
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins',
              fontSize: 16,
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
            unselectedLabelStyle: TextStyle(
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins',
              fontSize: 16,
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
            tabs: [
              Tab(
                child: Text(
                  TranslationBase.of(context).consultation,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 14,
                    letterSpacing: -0.36,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
              Tab(
                child: Text(
                  TranslationBase.of(context).logs,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 14,
                    letterSpacing: -0.36,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ],
          ),
          Expanded(
            child: TabBarView(
              physics: NeverScrollableScrollPhysics(),
              children: [
                isDataLoaded && !hasLiveCareRequest
                    ? ClinicList(
                        getLiveCareHistory: getLiveCareHistory,
                      )
                    : isDataLoaded
                        ? LiveCarePendingRequest(getLiveCareHistory: getLiveCareHistory, pendingERRequestHistoryList: pendingERRequestHistoryList)
                        : Container(),
                isDataLoaded
                    ? LiveCareLogs(
                        erRequestHistoryList: erRequestHistoryList,
                      )
                    : Container(),
              ],
              controller: _tabController,
            ),
          ),
        ]),
      ),
    );
  }

  void getLiveCareHistory() {
    GifLoaderDialogUtils.showMyDialog(context);
    setState(() {
      isDataLoaded = false;
      hasLiveCareRequest = false;
    });
    LiveCareService service = new LiveCareService();
    PatientERVirtualHistoryResponse patientERVirtualHistoryResponse = new PatientERVirtualHistoryResponse();
    service.getLivecareHistory(context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      setState(() {
        if (res['ErRequestHistoryList'].length != 0) {
          patientERVirtualHistoryResponse = PatientERVirtualHistoryResponse.fromJson(res);
          erRequestHistoryList = patientERVirtualHistoryResponse.erRequestHistoryList;

          if (patientERVirtualHistoryResponse.erRequestHistoryList[0].callStatus < 4) {
            pendingERRequestHistoryList = patientERVirtualHistoryResponse.erRequestHistoryList[0];
            hasLiveCareRequest = true;
          } else {
            hasLiveCareRequest = false;
          }
        }
        isDataLoaded = true;
      });
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }
}
