import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/pharmacies/LacumAccountInformation.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/lacum-viewmodel.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/pharmacies/widgets/lacum-banner-widget.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/buttons/borderedButton.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LakumSettingPage extends StatefulWidget {
  final LacumAccountInformation lacumInformation;
  final LacumAccountInformation lacumGroupInformation;

  LakumSettingPage(this.lacumInformation, this.lacumGroupInformation);

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

class _LakumSettingPageState extends State<LakumSettingPage> {
  bool _lakumAccountActivated = false;
  TextEditingController _nameController = new TextEditingController();
  TextEditingController _phoneController = new TextEditingController();

  ProjectViewModel? projectViewModel;

  @override
  void initState() {
    _lakumAccountActivated = widget.lacumGroupInformation
            .lakumInquiryInformationObjVersion!.accountStatus ==
        "Active";
    super.initState();
    _nameController.text = widget.lacumGroupInformation!.lakumInquiryInformationObjVersion!.memberName!;
    _phoneController.text = widget.lacumGroupInformation!.lakumInquiryInformationObjVersion!.mobileNumber!;
  }

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    projectViewModel = Provider.of(context);

    return BaseView<LacumViewModel>(
      onModelReady: (model) => model.setLakumData(
          widget.lacumInformation, widget.lacumGroupInformation),
      builder: (_, model, wi) => AppScaffold(
        appBarTitle: "${TranslationBase.of(context).lakum}",
        isShowAppBar: true,
        isShowDecPage: false,
        isPharmacy: true,
        backgroundColor: Colors.white,
        baseViewModel: model,
        body: Container(
          width: double.infinity,
          child: SingleChildScrollView(
            child: SizedBox(
                    height: mediaQuery.size.height -
                        60 -
                        mediaQuery.padding.top,
                    child: _buildSettingScreen(mediaQuery, model),
                  ),
          ),
        ),
      ),
    );
  }

  _buildSettingScreen(MediaQueryData mediaQuery, LacumViewModel model) {
    bool canUpdate = (_nameController.text != "" &&
        _phoneController.text != "" &&
        (_nameController.text != widget.lacumGroupInformation.lakumInquiryInformationObjVersion!.memberName ||
            _phoneController.text != widget.lacumGroupInformation.lakumInquiryInformationObjVersion!.mobileNumber));

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      mainAxisSize: MainAxisSize.max,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Stack(
              children: [
                Container(
                  height: mediaQuery.size.height * 0.15,
                  color: Colors.green,
                ),
                Column(
                  children: [
                    SizedBox(
                      height: mediaQuery.size.height * 0.05,
                    ),
                    Container(
                        width: mediaQuery.size.width * 1,
                        child: LakumBannerWidget(model, mediaQuery, false)),
                  ],
                )
              ],
            ),
            SizedBox(
              height: 20,
            ),
            SizedBox(
              height: 1,
              width: double.infinity,
              child: Container(
                color: Color(0xffefefef),
              ),
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Texts(TranslationBase.of(context).ActivateLAKUMAccount,
//                    "Activate LAKUM Account",
                    fontSize: 16,
                    fontWeight: FontWeight.normal,
                    color: Colors.black,
                  ),
                  Switch.adaptive(
                    activeColor: Color.fromRGBO(51, 153, 51, 1),
                    inactiveThumbColor: Colors.red,
                    activeTrackColor: Colors.grey,
                    inactiveTrackColor: Colors.grey,
                    value: _lakumAccountActivated,
                    onChanged: (val) {
                      if (_lakumAccountActivated) {
                        model.makeAccountDeactivate(projectViewModel!.user!.patientIdentificationNo!);
                      } else {
                        model.makeAccountActivate(projectViewModel!.user!.patientIdentificationNo!);
                      }
                      setState(() {
                        _lakumAccountActivated = val;
                      });
                    },
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 30,
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Texts(
                    TranslationBase.of(context).name,
                    fontSize: 13,
                    fontWeight: _lakumAccountActivated
                        ? FontWeight.bold
                        : FontWeight.normal,
                    color: Colors.grey.shade400,
                  ),
                  _lakumAccountActivated
                      ? TextField(
                          controller: _nameController,
                          enabled: _lakumAccountActivated ? true : false,
                          style: TextStyle(
                            fontSize: 16,
                            color: _lakumAccountActivated
                                ? Colors.black
                                : Colors.white38,
                          ),
                        )
                      : Texts(
                          _nameController.text,
                          fontSize: 16,
                          color: Colors.grey.shade600,
                        ),
                  SizedBox(
                    height: 10,
                  ),
                  Texts(
                    TranslationBase.of(context).mobileNumber,
                    fontSize: 13,
                    fontWeight: _lakumAccountActivated
                        ? FontWeight.bold
                        : FontWeight.normal,
                    color: Colors.grey.shade400,
                  ),
                  _lakumAccountActivated
                      ? TextField(
                          controller: _phoneController,
                          enabled: _lakumAccountActivated ? true : false,
                          keyboardType: TextInputType.phone,
                          style: TextStyle(
                            fontSize: 16,
                            color: _lakumAccountActivated
                                ? Colors.black
                                : Colors.white38,
                          ),
                        )
                      : Texts(
                          _phoneController.text,
                          fontSize: 16,
                          color: Colors.grey.shade600,
                        ),
                ],
              ),
            ),
          ],
        ),
        Container(
          child: Column(
            children: [
              SizedBox(
                height: 2,
                width: double.infinity,
                child: Container(
                  color: Color(0xffefefef),
                ),
              ),
              Container(
                margin: EdgeInsets.all(8),
                child: BorderedButton(
                  TranslationBase.of(context).save,
                  backgroundColor:
                      canUpdate ? Color(0xff60686b) : Color(0xffb0b4b5),
                  textColor: Colors.white,
                  fontSize: 16,
                  hPadding: 8,
                  vPadding: 12,
                  handler: canUpdate
                      ? () async {
                          GifLoaderDialogUtils.showMyDialog(context);
                         await model.createLakumAccount(_nameController.text, _phoneController.text);
                          GifLoaderDialogUtils.hideDialog(context);
                         if(model.state == ViewState.Idle){
                           AppToast.showSuccessToast(message: model.successMsg);
                           Navigator.pop(context, "");
                         } else if(model.state == ViewState.ErrorLocal){
                           AppToast.showErrorToast(message: model.error);
                         }
                        }
                      : (){},
                ),
              )
            ],
          ),
        ),
      ],
    );
  }
}
