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/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:smart_progress_bar/smart_progress_bar.dart';

class LiveCareHome extends StatefulWidget {
  static bool showFooterButton = true;

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

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

  bool isDataLoaded = false;
  bool hasLiveCareRequest = false;

  List<ErRequestHistoryList> erRequestHistoryList;

  ErRequestHistoryList pendingERRequestHistoryList;

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

    pendingERRequestHistoryList = new ErRequestHistoryList();

    WidgetsBinding.instance.addPostFrameCallback((_) => getLiveCareHistory());

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      appBarTitle: "LiveCare",
      isShowAppBar: true,
      body: Container(
        child: Column(children: [
          /// this is will not colored with theme data
          TabBar(
            tabs: [
              Tab(text: TranslationBase.of(context).consultation),
              Tab(text: TranslationBase.of(context).logs),
            ],
            controller: _tabController,
          ),
          Divider(
            color: Colors.grey[600],
            thickness: 0.5,
          ),
          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() {
    setState(() {
      isDataLoaded = false;
      hasLiveCareRequest = false;
    });
    LiveCareService service = new LiveCareService();
    PatientERVirtualHistoryResponse patientERVirtualHistoryResponse =
        new PatientERVirtualHistoryResponse();
    service
        .getLivecareHistory(context)
        .then((res) {
          setState(() {
            print(res['ErRequestHistoryList'].length);
            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;
              }
            }
          });
        })
        .catchError((err) {
          print(err);
        })
        .showProgressBar(
            text: "Loading", backgroundColor: Colors.blue.withOpacity(0.6))
        .then((value) {
          setState(() {
            isDataLoaded = true;
          });
        });
  }
}
