import 'package:diplomaticquarterapp/core/service/medical/labs_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/custom_text_button.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';

class PassportUpdatePage extends StatefulWidget {
  @override
  _PassportUpdatePageState createState() => _PassportUpdatePageState();
}

class _PassportUpdatePageState extends State<PassportUpdatePage> {
  TextEditingController passportNumber = new TextEditingController();
  bool _isButtonDisabled;
  ProjectViewModel projectViewModel;

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) => getPassportNumber());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).passportNumber,
      isShowAppBar: true,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      backgroundColor: CustomColors.appBackgroudGrey2Color,
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: double.infinity,
                        child: Container(
                          decoration: cardRadius(12),
                          margin: EdgeInsets.zero,
                          child: Padding(
                            padding: const EdgeInsets.all(12.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  TranslationBase.of(context).covidBookAppo,
                                  style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 16,
                                    letterSpacing: -0.64,
                                  ),
                                ),
                                mHeight(6),
                                Row(
                                  children: [
                                    Text(
                                      TranslationBase.of(context).patientName + ":",
                                      style: TextStyle(
                                        fontWeight: FontWeight.w600,
                                        fontSize: 10,
                                        letterSpacing: -0.6,
                                        color: CustomColors.grey,
                                      ),
                                    ),
                                    mWidth(3),
                                    Text(
                                      projectViewModel.user.firstName + " " + projectViewModel.user.lastName,
                                      style: TextStyle(
                                        fontWeight: FontWeight.w600,
                                        fontSize: 12,
                                        letterSpacing: -0.48,
                                      ),
                                    ),
                                  ],
                                ),
                                Row(
                                  children: [
                                    Text(
                                      TranslationBase.of(context).identificationNumber + ":",
                                      style: TextStyle(
                                        fontWeight: FontWeight.w600,
                                        fontSize: 10,
                                        letterSpacing: -0.6,
                                        color: CustomColors.grey,
                                      ),
                                    ),
                                    mWidth(3),
                                    Text(
                                      projectViewModel.user.patientIdentificationNo,
                                      style: TextStyle(
                                        fontWeight: FontWeight.w600,
                                        fontSize: 12,
                                        letterSpacing: -0.48,
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      mHeight(40),
                      SvgPicture.asset("assets/images/new/passport.svg", width: 40.0, fit: BoxFit.fill),
                      mHeight(12),
                      Text(
                        TranslationBase.of(context).enterPassportNumber,
                        textAlign: TextAlign.start,
                        style: TextStyle(
                          fontSize: 16.0,
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                          letterSpacing: -0.64,
                        ),
                      ),
                      mHeight(8),
                      inputWidget(TranslationBase.of(context).passportNumber, "", passportNumber),
                    ],
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 12.0, left: 12.0, right: 12.0, bottom: 10.0),
              child: ButtonTheme(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10.0),
                ),
                minWidth: MediaQuery.of(context).size.width,
                height: 45.0,
                child: CustomTextButton(
                  backgroundColor: Color(0xFFc5272d),
                  disabledForegroundColor: Colors.red[300].withOpacity(0.38),
                  disabledBackgroundColor: Colors.red[300].withOpacity(0.12),
                  elevation: 0,
                  onPressed: () {
                    if (_isButtonDisabled == false)
                      updatePassportNumber();
                    else
                      AppToast.showErrorToast(message: TranslationBase.of(context).validPassportNumber);
                  },
                  child: Text(
                    TranslationBase.of(context).submit,
                    style: TextStyle(
                      fontSize: 16.0,
                      letterSpacing: -0.64,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget inputWidget(String _labelText, String _hintText, TextEditingController _controller, {String prefix, bool isEnable = true, bool hasSelection = false}) {
    return Container(
      padding: EdgeInsets.only(left: 16, right: 16, bottom: 15, top: 15),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        color: Colors.white,
        border: Border.all(
          color: Color(0xffefefef),
          width: 1,
        ),
      ),
      child: InkWell(
        onTap: hasSelection ? () {} : null,
        child: Row(
          children: [
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    _labelText,
                    style: TextStyle(
                      fontSize: 11,
                      fontWeight: FontWeight.w600,
                      color: Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                  ),
                  TextField(
                    enabled: isEnable,
                    scrollPadding: EdgeInsets.zero,
                    keyboardType: TextInputType.text,
                    controller: _controller,
                    onChanged: (value) => {_onPassportTextChanged(value)},
                    style: TextStyle(
                      fontSize: 14,
                      height: 21 / 14,
                      fontWeight: FontWeight.w400,
                      color: Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                    decoration: InputDecoration(
                      isDense: true,
                      hintText: _hintText,
                      hintStyle: TextStyle(
                        fontSize: 14,
                        height: 21 / 14,
                        fontWeight: FontWeight.w400,
                        color: Color(0xff575757),
                        letterSpacing: -0.56,
                      ),
                      prefixIconConstraints: BoxConstraints(minWidth: 50),
                      prefixIcon: prefix == null
                          ? null
                          : Text(
                              "+" + prefix,
                              style: TextStyle(
                                fontSize: 14,
                                height: 21 / 14,
                                fontWeight: FontWeight.w500,
                                color: Color(0xff2E303A),
                                letterSpacing: -0.56,
                              ),
                            ),
                      contentPadding: EdgeInsets.zero,
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      enabledBorder: InputBorder.none,
                    ),
                  ),
                ],
              ),
            ),
            if (hasSelection) Icon(Icons.keyboard_arrow_down_outlined),
          ],
        ),
      ),
    );
  }

  _onPassportTextChanged(content) {
    if (content.length >= 1) {
      setState(() {
        _isButtonDisabled = false;
      });
    } else {
      setState(() {
        _isButtonDisabled = true;
      });
    }
  }

  void updatePassportNumber() {
    LabsService service = new LabsService();
    GifLoaderDialogUtils.showMyDialog(context);

    service.updateCovidPassportNumber(passportNumber.text).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      Navigator.of(context).pop(true);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  void getPassportNumber() {
    LabsService service = new LabsService();
    GifLoaderDialogUtils.showMyDialog(context);

    service.getCovidPassportNumber().then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['Covid19_Certificate_GetPassportList'].length != 0) {
        passportNumber.text = res['Covid19_Certificate_GetPassportList'][0]['PassportNo'];
        if (res['Covid19_Certificate_GetPassportList'][0]['PassportNo'] != "") {
          _isButtonDisabled = false;
        }
      } else {
        passportNumber.text = "";
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }
}
