import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/livecare/pharma_livecare_intro_page.dart';
import 'package:diplomaticquarterapp/services/livecare_services/livecare_provider.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.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:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';

class LiveCareTypeSelect extends StatefulWidget {
  @override
  _LiveCareTypeSelectState createState() => _LiveCareTypeSelectState();
}

class _LiveCareTypeSelectState extends State<LiveCareTypeSelect> {
  var languageID;
  AppSharedPreferences sharedPref = AppSharedPreferences();

  ProjectViewModel projectViewModel;
  String pharmacyLiveCareQRCode = "";

  @override
  void initState() {
    getLanguageID();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of<ProjectViewModel>(context);
    return AppScaffold(
      isShowDecPage: true,
      isShowAppBar: true,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      appBarTitle: TranslationBase.of(context).livecare.toUpperCase(),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(child: Text(TranslationBase.of(context).livecareService, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, letterSpacing: -0.64, color: CustomColors.darkGrey))),
              Container(
                  margin: EdgeInsets.only(top: 7.0),
                  child: Text(TranslationBase.of(context).livecareServiceDesc, style: TextStyle(fontSize: 14.0, letterSpacing: -0.64, color: CustomColors.textColor, fontWeight: FontWeight.w600))),
              Container(
                  margin: EdgeInsets.only(top: 20.0),
                  child: Text(TranslationBase.of(context).whyLivecare, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, letterSpacing: -0.64, color: CustomColors.darkGrey))),
              Container(
                child: Row(
                  children: <Widget>[
                    SvgPicture.asset("assets/images/new-design/allow.svg", width: 20),
                    Container(
                      width: MediaQuery.of(context).size.width * 0.72,
                      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
                      child: Text(TranslationBase.of(context).livecarePoint1,
                          overflow: TextOverflow.clip, style: TextStyle(fontSize: 12.0, letterSpacing: -0.64, color: CustomColors.black, fontWeight: FontWeight.w600)),
                    )
                  ],
                ),
              ),
              Container(
                child: Row(
                  children: <Widget>[
                    SvgPicture.asset("assets/images/new-design/allow.svg", width: 20),
                    Container(
                      width: MediaQuery.of(context).size.width * 0.72,
                      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
                      child: Text(TranslationBase.of(context).docVirtualAppoIns4,
                          overflow: TextOverflow.clip, style: TextStyle(fontSize: 12.0, letterSpacing: -0.64, color: CustomColors.black, fontWeight: FontWeight.w600)),
                    )
                  ],
                ),
              ),
              Container(
                child: Row(
                  children: <Widget>[
                    SvgPicture.asset("assets/images/new-design/allow.svg", width: 20),
                    Container(
                      width: MediaQuery.of(context).size.width * 0.72,
                      margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0.0),
                      child: Text(TranslationBase.of(context).docVirtualAppoIns5,
                          overflow: TextOverflow.clip, style: TextStyle(fontSize: 12.0, letterSpacing: -0.64, color: CustomColors.black, fontWeight: FontWeight.w600)),
                    )
                  ],
                ),
              ),
              Container(
                  margin: EdgeInsets.only(top: 20.0),
                  child: Text(TranslationBase.of(context).livecareSummary, style: TextStyle(fontSize: 14.0, letterSpacing: -0.64, color: CustomColors.textColor, fontWeight: FontWeight.w600))),
              GridView(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, crossAxisSpacing: 13, mainAxisSpacing: 9),
                physics: NeverScrollableScrollPhysics(),
                padding: EdgeInsets.only(top: 40.0),
                shrinkWrap: true,
                children: [
                  _loginOptionButton(TranslationBase.of(context).livecareOption1, 'assets/images/new/Live_Care.svg', 1),
                  _loginOptionButton(TranslationBase.of(context).livecareOption4, 'assets/images/new/book appointment.svg', 2),
                  _loginOptionButton(TranslationBase.of(context).pharmaLiveCare, 'assets/images/new/pharma.svg', 3, isEnable: projectViewModel.havePrivilege(99)),
                ],
              ),
              SizedBox(
                height: 40.0,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _loginOptionButton(String _title, String _icon, int _loginIndex, {bool isEnable = true}) {
    return InkWell(
      onTap: () {
        if (_loginIndex == 1) {
          Navigator.pop(context, "immediate");
          projectViewModel.analytics.liveCare.livecare_immediate_consultation();
        } else if (_loginIndex == 2) {
          Navigator.pop(context, "schedule");
          projectViewModel.analytics.liveCare.livecare_schedule_video_call();
        } else {
          //Pharmacy LiveCare
          if (isEnable) {
            Navigator.push(
              context,
              FadePage(
                page: PharmaLiveCareIntroPage(),
              ),
            ).then((value) {
              if (value != null && value.contains("pharmacy/")) {
                pharmacyLiveCareQRCode = value.split("/")[1];
                startPharmacyLiveCareProcess();
              }
            });
          }
        }
      },
      child: Stack(children: [
        AspectRatio(
          aspectRatio: 1.0,
          child: Container(
            padding: EdgeInsets.only(left: 20, right: 20, bottom: 3, top: 28),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              color: Colors.white,
              border: Border.all(
                color: Color(0xffefefef),
                width: 1,
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                SvgPicture.asset(
                  _icon,
                  height: _loginIndex == 3 ? 80 : 60,
                  width: _loginIndex == 3 ? 80 : 60,
                ),
                Text(
                  _title,
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48, height: 20 / 16),
                ),
              ],
            ),
          ),
        ),
        isEnable
            ? Container()
            : Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(15),
                  color: Colors.grey.withOpacity(0.6),
                  border: Border.all(
                    color: Color(0xffefefef),
                    width: 1,
                  ),
                ),
                width: double.infinity,
                height: double.infinity,
                child: Icon(
                  Icons.lock_outline,
                  size: 40,
                ),
              )
      ]),
    );
  }

  readQRCode() async {
    pharmacyLiveCareQRCode = (await BarcodeScanner.scan())?.rawContent;
    if (pharmacyLiveCareQRCode != "") {
      GifLoaderDialogUtils.showMyDialog(context);
      LiveCareService service = new LiveCareService();
      service.getPatientInfoByQR(pharmacyLiveCareQRCode, context).then((res) {
        GifLoaderDialogUtils.hideDialog(context);
      }).catchError((err) {
        GifLoaderDialogUtils.hideDialog(context);
        print(err);
        startPharmacyLiveCareProcess();
      });
    } else {}
  }

  startPharmacyLiveCareProcess() {
    sharedPref.setString(LIVECARE_CLINIC_DATA, "Pharmacy LiveCare" + "-" + "501" + "-" + "1");
    Navigator.pop(context, "pharmacy/$pharmacyLiveCareQRCode");
    // Navigator.push(context, FadePage(page: LiveCareHome(isPharmacyLiveCare: true, pharmacyLiveCareQRCode: pharmacyLiveCareQRCode,)));
  }

  getLanguageID() async {
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    setState(() {
      this.languageID = languageID;
    });
  }
}
