import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/H2O_view_model.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/h2o/Dialog/confirm_add_amount_dialog.dart';
import 'package:diplomaticquarterapp/pages/medical/balance/new_text_Field.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'Dialog/select_amount_dialog.dart';

class AddCustomAmount extends StatefulWidget {
  final H2OViewModel model;
  final Function changePageViewIndex;

  const AddCustomAmount({Key key, this.model, this.changePageViewIndex})
      : super(key: key);

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

class _AddCustomAmountState extends State<AddCustomAmount> {
  TextEditingController _nameTextController = TextEditingController();
  AmountModel selectedAmount;

  @override
  void initState() {
    setState(() {
      _nameTextController.text = "0";
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      appBarTitle:TranslationBase.of(context).customLabel,
      body: SingleChildScrollView(
        physics: ScrollPhysics(),
        child: Container(
          margin: EdgeInsets.all(12),
          child: Center(
            child: FractionallySizedBox(
              widthFactor: 0.94,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 12,
                  ),
                  NewTextFields(
                    hintText: TranslationBase.of(context).h2oAmountOfWater,
                    // type: "Number",
                    controller: _nameTextController,
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  InkWell(
                    onTap: () => confirmAmountTypeDialog(),
                    child: Container(
                      padding: EdgeInsets.all(12),
                      width: double.infinity,
                      height: 65,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(12),
                          color: Colors.white),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Texts(getAmountName()),
                          Icon(Icons.arrow_drop_down)
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  SecondaryButton(
                      textColor: Colors.white,
                      label: TranslationBase.of(context).ok,
                      onTap: () async {
                        Navigator.of(context).pop();
                        showConfirmMessage (int.parse(_nameTextController.text), widget.model);
                      },
                      // loading: model.state == ViewState.BusyLocal,
                      disabled: _nameTextController.text.isEmpty || selectedAmount == null),
                  SizedBox(
                    height: 12,
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }


void confirmAmountTypeDialog() {
  showDialog(
    context: context,
    child: SelectAmountDialog(
      selectedAmount: selectedAmount,
      onValueSelected: (value) {
        setState(() {
          selectedAmount = value;
        });
      },
    ),
  );
}


  String getAmountName() {
    if (selectedAmount != null)
      return selectedAmount.name;
    else
      return TranslationBase.of(context).selectUnit;
  }



  void showConfirmMessage(int amount, H2OViewModel model) {
    showDialog(context: context, child: ConfirmAddAmountDialog(model: model,amount:amount,));
  }
}
