import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/blood_sugar_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 AddBloodSugarPage extends StatefulWidget {
  @override
  _AddBloodSugarPageState createState() => _AddBloodSugarPageState();
}

class _AddBloodSugarPageState extends State<AddBloodSugarPage> {
  TextEditingController _bloodSugarValueController = TextEditingController();
  DateTime bloodSugarDate = DateTime.now();
  DateTime timeSugarDate = DateTime.now();
  String measureUnitSelectedType = 'mg/dlt';
  int measuredTime=1;
  final List<String> measureUnitList = ['mg/dlt', 'mol/L'];
  final List<String> measureTimeEnList = [
    'Before Breakfast',
    'After Breakfast',
    'Before Lunch',
    'After Lunch',
    'Before Dinner',
    'After Dinner',
    'Before Sleep',
    'After Sleep',
    'Fasting',
    'Other',
  ];
  final List<String> measureTimeArList = [
    'Before Breakfast',
    'After Breakfast',
    'Before Lunch',
    'After Lunch',
    'Before Dinner',
    'After Dinner',
    'Before Sleep',
    'After Sleep',
    'Fasting',
    'Other',
  ];
  String measureTimeSelectedType;


  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    showTaskOptions() {
      showModalBottomSheet(
          backgroundColor: Colors.white,
          context: context,
          builder: (BuildContext bc) {
            return Container(
              padding: EdgeInsets.symmetric(vertical: 12.0),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(16.0),
                      topRight: Radius.circular(16.0))),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                        color: Colors.grey[200],
                        borderRadius: BorderRadius.circular(3.0)),
                    width: 40.0,
                    height: 6.0,
                  ),
                  InkWell(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Padding(
                      padding: EdgeInsets.symmetric(
                          horizontal: 18.0, vertical: 18.0),
                      child: Row(
                        children: <Widget>[
                          Icon(
                            FeatherIcons.share,
                            color: Theme
                                .of(context)
                                .primaryColor,
                            size: 18.0,
                          ),
                          SizedBox(width: 24.0),
                          Texts('Share Task',
                              variant: "body2Link", color: Colors.grey[800]),
                        ],
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      Navigator.pop(context);
                      // Navigator.of(context).push(SlideUpPageRoute(widget: PostTaskIndex(task: new Task(category: task?.category, description: task?.description, title: task?.title))));
                    },
                    child: Padding(
                      padding: EdgeInsets.symmetric(
                          horizontal: 18.0, vertical: 18.0),
                      child: Row(
                        children: <Widget>[
                          Icon(
                            FeatherIcons.copy,
                            color: Theme
                                .of(context)
                                .primaryColor,
                            size: 18.0,
                          ),
                          SizedBox(width: 24.0),
                          Texts('Post Similar Task',
                              variant: "body2Link", color: Colors.grey[800]),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            );
          });
    }

    return BaseView<BloodSugarViewMode>(
      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 Blood Sugar Value',
                      controller: _bloodSugarValueController,
                      keyboardType: TextInputType.number,
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    InkWell(
                      onTap: () {
                        confirmSelectMeasureUnitDialog();
                      },
                      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(measureUnitSelectedType),
                            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())],
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    InkWell(
                      onTap: () {
                        confirmSelectMeasureTimeDialog(projectViewModel.isArabic
                            ? measureTimeEnList
                            : measureTimeArList);
                      },
                      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 ?? 'Others'),
                            Icon(
                              Icons.arrow_drop_down,
                              color: Colors.grey,
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            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 (_bloodSugarValueController.text.isNotEmpty) {
                    model.addDiabtecResult(diabtecUnit: measureUnitSelectedType,
                        measuredTime: measuredTime,
                        bloodSugerResult:_bloodSugarValueController.text.toString(),
                      bloodSugerDateChart: '${bloodSugarDate.year}-${bloodSugarDate.month}-${bloodSugarDate.day} ${timeSugarDate.hour}:${timeSugarDate.minute}:00',

                    );
                  }
                }),
              ),
            ),
          ),
    );
  }

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

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

  void confirmSelectMeasureUnitDialog() {
    showDialog(
      context: context,
      child: RadioStringDialog(
        radioList: measureUnitList,
        title: 'Measure unit',
        selectedValue: measureUnitSelectedType,
        onValueSelected: (value) {
          setState(() {
            measureUnitSelectedType = value;
          });
        },
      ),
    );
  }

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