import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/blood_pressure_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/blood_sugar_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/weight_pressure_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/balance/new_text_Field.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/RadioStringDialog.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:feather_icons_flutter/feather_icons_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:provider/provider.dart';

class AddWeightPage extends StatefulWidget {
  @override
  _AddWeightPageState createState() => _AddWeightPageState();
}

class _AddWeightPageState extends State<AddWeightPage> {
  TextEditingController _weightValueController = TextEditingController();
  DateTime bloodSugarDate = DateTime.now();
  DateTime timeSugarDate = DateTime.now();
  int weightUnit = 1;
  final List<String> measureUnitEnList = [
    'Kg',
    'Pound',
  ];
  final List<String> measureUnitArList = [
    'Kg',
    'Pound',
  ];
  String measureTimeSelectedType = 'Kg';


  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    return BaseView<WeightPressureViewMode>(
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: 'Add',
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Container(
            margin: EdgeInsets.all(15),
            child: Column(
              children: [
                SizedBox(
                  height: 15,
                ),
                NewTextFields(
                  hintText: 'Enter Weight Value',
                  controller: _weightValueController,
                  keyboardType: TextInputType.number,
                ),
                SizedBox(
                  height: 8,
                ),
                InkWell(
                  onTap: () {
                    confirmSelectMeasureTimeDialog(projectViewModel.isArabic
                        ? measureUnitEnList
                        : measureUnitArList);
                  },
                  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(measureTimeSelectedType),
                        Icon(
                          Icons.arrow_drop_down,
                          color: Colors.grey,
                        )
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 8,
                ),
                InkWell(
                  onTap: () {
                    DatePicker.showDatePicker(context,
                        showTitleActions: true,
                        minTime: DateTime(DateTime.now().year - 1, 1, 1),
                        maxTime: DateTime.now(), onConfirm: (date) {
                      print('confirm $date');
                      setState(() {
                        bloodSugarDate = date;
                      });
                    },
                        currentTime: bloodSugarDate,
                        locale: projectViewModel.localeType);
                  },
                  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('Date'),
                        Texts(getDate()),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 8,
                ),
                InkWell(
                  onTap: () {
                    DatePicker.showTimePicker(context, showTitleActions: true,
                        onConfirm: (date) {
                      print('confirm $date');
                      setState(() {
                        timeSugarDate = date;
                      });
                    },
                        currentTime: timeSugarDate,
                        locale: projectViewModel.localeType);
                  },
                  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('Time'), Texts(getTime())],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        bottomSheet: Container(
          color: Colors.transparent,
          width: double.infinity,
          height: MediaQuery.of(context).size.width * 0.2,
          child: Padding(
            padding: const EdgeInsets.all(15.0),
            child: SecondaryButton(
                loading: model.state == ViewState.BusyLocal,
                label: 'SAVE',
                textColor: Colors.white,
                onTap: () {
                  if (_weightValueController.text.isNotEmpty ) {
                    model.addWeightResult(
                      weightDate: '${bloodSugarDate.year}-${bloodSugarDate.month}-${bloodSugarDate.day} ${timeSugarDate.hour}:${timeSugarDate.minute}:00',
                      weightMeasured: _weightValueController.text.toString(),
                      weightUnit: weightUnit,
                    );
                  }
                }),
          ),
        ),
      ),
    );
  }

  String getDate() {
    return "${DateUtil.getMonth(bloodSugarDate.month)} ${bloodSugarDate.day}, ${bloodSugarDate.year}";
  }

  String getTime() {
    return " ${timeSugarDate.hour}:${timeSugarDate.minute}";
  }

  void confirmSelectMeasureTimeDialog(List<String> list) {
    showDialog(
      context: context,
      child: RadioStringDialog(
        radioList: list,
        title: 'Measure unit',
        selectedValue: measureTimeSelectedType,
        onValueSelected: (value) {
          setState(() {
            measureTimeSelectedType = value;
            weightUnit = list.indexOf(value);
          });
        },
      ),
    );
  }
}
