import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

const activeCardColor = CustomColors.green;
const inactiveCardColor = CustomColors.white;

class BloodCholesterol extends StatefulWidget {
  @override
  _BloodCholesterolState createState() => _BloodCholesterolState();
}

class _BloodCholesterolState extends State<BloodCholesterol> {
  Color cardMGColor = inactiveCardColor;
  Color cardMMOLColor = inactiveCardColor;

  void updateColor(int type) {
    if (type == 1) {
      if (cardMGColor == inactiveCardColor) {
        cardMGColor = activeCardColor;
        cardMMOLColor = inactiveCardColor;
      } else {
        cardMGColor = inactiveCardColor;
      }
    }
    if (type == 2) {
      if (cardMMOLColor == inactiveCardColor) {
        cardMMOLColor = activeCardColor;
        cardMGColor = inactiveCardColor;
      } else {
        cardMMOLColor = inactiveCardColor;
      }
    }
  }

  void calculateBloodCholesterol() {
    if (cardMGColor == activeCardColor) {
      inputValue = double.parse(textController.text);
      inputValue = inputValue / conversionConst;
      unit = 'mmol/l';
    } else if (cardMMOLColor == activeCardColor) {
      inputValue = double.parse(textController.text);
      inputValue = inputValue * conversionConst;
      unit = 'mg/dlt';
    }
  }

  bool _visible = false;
  TextEditingController textController = new TextEditingController();
  TextEditingController resultTextController = new TextEditingController();
  String finalValue;

  double inputValue = 0.0;
  String unit;
  double conversionConst = 38.67;

  @override
  void initState() {
    updateColor(1);
    unit = 'mmol/l';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      appBarTitle: TranslationBase.of(context).cholesTitle,
      isShowDecPage: false,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(child: Text(TranslationBase.of(context).cholesTitle, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, letterSpacing: -0.64, color: CustomColors.darkGrey))),
              Container(
                margin: EdgeInsets.only(top: 10.0),
                child: Text(TranslationBase.of(context).convertCholesterolStatement, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 14.0, letterSpacing: -0.64, color: CustomColors.darkGrey)),
              ),
              SizedBox(
                height: 13.0,
              ),
              Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(top: 12.0),
                      child: Text(TranslationBase.of(context).convertFrom, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 16.0, letterSpacing: -0.64, color: CustomColors.darkGrey)),
                    ),
                    SizedBox(
                      height: 9.0,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                updateColor(1);
                                inputValue = double.parse(textController.text);
                                inputValue = inputValue / conversionConst;
                                unit = 'mmol/l';
                              });
                            },
                            child: Container(
                              clipBehavior: Clip.antiAlias,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(
                                  Radius.circular(6.0),
                                ),
                                border: Border.all(width: 2, color: cardMGColor),
                                boxShadow: [
                                  BoxShadow(
                                    color: Color(0xff000000).withOpacity(.05),
                                    blurRadius: 27,
                                    offset: Offset(0, -3),
                                  ),
                                ],
                                color: Colors.white,
                              ),
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Text('MG/DLt to MMOL/L',
                                    textAlign: TextAlign.center, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 13.0, letterSpacing: -0.64, color: CustomColors.darkGrey)),
                              ),
                            ),
                          ),
                        ),
                        SizedBox(
                          width: 12.0,
                        ),
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                updateColor(2);
                                inputValue = double.parse(textController.text);
                                inputValue = inputValue * conversionConst;
                                unit = 'mg/dlt';
                              });
                            },
                            child: Container(
                              clipBehavior: Clip.antiAlias,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(
                                  Radius.circular(6.0),
                                ),
                                border: Border.all(width: 2, color: cardMMOLColor),
                                boxShadow: [
                                  BoxShadow(
                                    color: Color(0xff000000).withOpacity(.05),
                                    blurRadius: 27,
                                    offset: Offset(0, -3),
                                  ),
                                ],
                                color: Colors.white,
                              ),
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Text('MMOL/L to MG/DLt',
                                    textAlign: TextAlign.center, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 13.0, letterSpacing: -0.64, color: CustomColors.darkGrey)),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 12.0,
              ),
              inputWidget(TranslationBase.of(context).enterReadingValue, '0.0', textController),
              SizedBox(
                height: 12.0,
              ),
              Visibility(
                visible: _visible,
                child: inputWidget(TranslationBase.of(context).result, inputValue.toStringAsFixed(2) + " " + unit.toString(), resultTextController, isEnable: false),
              ),
              SizedBox(
                height: 15.0,
              ),
            ],
          ),
        ),
      ),
      bottomSheet: Container(
        color: CustomColors.white,
        padding: EdgeInsets.fromLTRB(12.0, 1.0, 12.0, 25.0),
        child: DefaultButton(
          TranslationBase.of(context).calculate,
          () {
            setState(() {
              _visible == false ? _visible = !_visible : _visible = _visible;
              calculateBloodCholesterol();
            });
          },
          color: CustomColors.green,
        ),
      ),
    );
  }

  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(10),
        color: isEnable ? Colors.white : CustomColors.grey2.withOpacity(0.4),
        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.number,
                    controller: _controller,
                    onChanged: (value) => {},
                    style: TextStyle(
                      fontSize: 14,
                      height: 21 / 14,
                      fontWeight: FontWeight.w400,
                      color: Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                    decoration: InputDecoration(
                      isDense: true,
                      hintText: _hintText,
                      fillColor: CustomColors.accentColor,
                      hintStyle: TextStyle(
                        fontSize: isEnable ? 14 : 24,
                        height: 21 / 14,
                        fontWeight: FontWeight.w400,
                        color: isEnable ? Color(0xff575757) : CustomColors.black,
                        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),
          ],
        ),
      ),
    );
  }
}
