import 'package:device_calendar/device_calendar.dart';
import 'package:diplomaticquarterapp/core/model/childvaccines/List_BabyInformationModel.dart';
import 'package:diplomaticquarterapp/core/model/childvaccines/add_newchild_model.dart';
import 'package:diplomaticquarterapp/core/model/childvaccines/create_new_user_model.dart';
import 'package:diplomaticquarterapp/core/model/childvaccines/user_information_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/child_vaccines/add_new_child_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/child_vaccines/child_vaccines_view_model.dart';
import 'package:diplomaticquarterapp/pages/Blood/new_text_Field.dart';
import 'package:diplomaticquarterapp/pages/ChildVaccines/add_newchild_page.dart';
import 'package:diplomaticquarterapp/pages/ChildVaccines/child_page.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/active_medications/DayCheckBoxDialog.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.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:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';

enum Gender { Male, Female, NON }
enum BeneficiaryType { MyAccount, MyFamilyFiles, OtherAccount, NON }

class AddNewChildPage extends StatefulWidget {
  final int frequency;
  final int days;
  final String itemDescription;
  String dateAdd;

  List<DateTime> _scheduleList = List();
  List<DayOfWeek> daysOfWeek = [
    DayOfWeek.Monday,
    DayOfWeek.Tuesday,
    DayOfWeek.Wednesday,
    DayOfWeek.Thursday,
    DayOfWeek.Friday,
    DayOfWeek.Saturday,
    DayOfWeek.Sunday
  ];

  DateTime startDay;
  DateTime endDay;

  //AddNewChildPage({Key key, this.frequency, this.days, this.itemDescription}) : super(key: key);
  AddNewChildPage({Key key, this.frequency, this.days, this.itemDescription}) {
    startDay = DateTime.now();
    endDay =
        DateTime.now(); //endDay = DateTime.now().add(Duration(days: days));
    int hour = 24; //(24 / frequency).round();
    int durations = 24 ~/ hour;
    for (int count = 0; count < durations; count++) {
      _scheduleList.add(DateTime(DateTime.now().year, DateTime.now().month,
          DateTime.now().day, (hour * count)));
    }
  }

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

class _AddNewChildPageState extends State<AddNewChildPage> {
  int tappedIndex;
  int checkedValue;

  @override
  void initState() {
    super.initState();
    tappedIndex = -1;
  }

  TextEditingController _firstTextController = TextEditingController();
  TextEditingController _secondTextController = TextEditingController();
  TextEditingController _notesTextController = TextEditingController();
  BeneficiaryType beneficiaryType = BeneficiaryType.NON;
  Gender gender = Gender.Male;
  CreateNewUser_New newUserChild = CreateNewUser_New();

  //ChildVaccinesViewModel addvancedModel = ChildVaccinesViewModel();
  List_BabyInformationModel addvancedModel = List_BabyInformationModel();
  CreateNewBaby newChild = CreateNewBaby();
  List_UserInformationModel informationModel = List_UserInformationModel();

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;

    return BaseView<AddNewChildViewModel>(
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).vaccination,
        body: SingleChildScrollView(
          physics: ScrollPhysics(),
          child: Container(
            margin: EdgeInsets.all(12),
            child: Column(
              // crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: 20,
                ),
                Texts(
                  TranslationBase.of(context).vaccinationAddChildMsg,
                  //+model.user.firstName,
                  textAlign: TextAlign.center,
                ),
                SizedBox(
                  height: 12,
                ),
                NewTextFields(
                  hintText: TranslationBase.of(context).firstName,
                  controller: _firstTextController,
                ),
                SizedBox(
                  height: 12,
                ),
                NewTextFields(
                  hintText: TranslationBase.of(context).middleName,
                  controller: _secondTextController,
                ),
                SizedBox(
                  height: 12,
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      TranslationBase.of(context).gender,
                      textAlign: TextAlign.end,
                    ),
                  ],
                ),
                Container(
                  width: double.infinity,
                  height: size.height * 0.12,
                  padding: EdgeInsets.all(12),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Container(
                          color: Colors.white,
                          child: SecondaryButton(
                            textColor:
                                checkedValue == 1 ? Colors.white : Colors.black,
                            color: checkedValue == 1 ? Colors.red : Colors.white,
                            label: TranslationBase.of(context).male,
                            onTap: () {
                              setState(() {
                                checkedValue = 1;
                                print("checkedValue=" + checkedValue.toString());
                              });

                              // bloodDetails.
                            },
                          ),
                        ),
                      ),
                      Expanded(
                        child: Container(
                          color: Colors.white,
                          child: SecondaryButton(
                            textColor:
                                checkedValue == 2 ? Colors.white : Colors.black,
                            color: checkedValue == 2 ? Colors.red : Colors.white,
                            label: TranslationBase.of(context).female,
                            //
                            onTap: () {
                              setState(() {
                                checkedValue = 2;
                                print("checkedValue=" + checkedValue.toString());
                              });
                              // bloodDetails.city=_selectedHospital.toString();

                              // bloodDetails.
                            },
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                //==========
                SizedBox(
                  height: 6,
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      TranslationBase.of(context).dob,
                      textAlign: TextAlign.end,
                    ),
                  ],
                ),
                InkWell(
                  onTap: () {
                    DatePicker.showDatePicker(
                      context,
                      showTitleActions: true,
                      // minTime: DateTime(
                      //     DateTime.now().year, DateTime.now().month - 1, 1),
                      minTime: DateTime(1, 1, 1),
                      maxTime: DateTime.now(),
                      onConfirm: (date) {
                        setState(() {
                          widget.startDay = date;
                        });
                      },
                      currentTime: widget.startDay,
                      // 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(//getStartDay()
                            // DateUtil.yearMonthDay(DateTime.now())
                            getStartDay()),
                        Icon(
                          Icons.calendar_today,
                          color: Colors.black,
                        )
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 12,
                ),
                Container(
                  height: MediaQuery.of(context).size.height * 0.12,
                  width: double.infinity,
                  padding: EdgeInsets.all(15),
                  child: SecondaryButton(
                    textColor: Colors.white,
                    color: checkedValue == false
                        ? Colors.white24
                        : Color.fromRGBO(
                            63,
                            72,
                            74,
                            1,
                          ),
                    label: TranslationBase.of(context).add,
                    //
                    onTap: () async {
                      newChild.babyName = _firstTextController.text +
                          " " +
                          _secondTextController.text;
                      newChild.gender = checkedValue.toString();
                      newChild.strDOB = getStartDay();
                      newChild.tempValue = true;
                      newChild.isLogin = true;

                      await model.createNewBabyOrders(newChild: newChild);
                      if (model.isAdded) {
                        AppToast.showSuccessToast(message: TranslationBase.of(context).childAddedSuccessfully);
                        Navigator.pop(context, model.isAdded);
                      } else {
                        //TODO handling error
                      }
                    },
                  ),
                ),
                //=========
              ],
            ),
          ),
        ),
        // bottomSheet:
      ),
    );
  }

  String getStartDay() {
    return "${DateUtil.getMonth(widget.startDay.month)} ${widget.startDay.day}, ${widget.startDay.year}";
  }

  String getEndDay() {
    return "${DateUtil.getMonth(widget.endDay.month)} ${widget.endDay.day}, ${widget.endDay.year}";
  }

  String getDateTime(DateTime dateTime) {
    return '${dateTime.hour}:${dateTime.minute}';
  }

  String getDays() {
    String days = "";
    widget.daysOfWeek.forEach((element) {
      days += "${DateUtil.getDay(element)},";
    });
    return days;
  }

  void confirmSelectDayDialog() {
    showDialog(
      context: context,
      child: DayCheckBoxDialog(
        title: 'Select Day',
        selectedDaysOfWeek: widget.daysOfWeek,
        onValueSelected: (value) {
          setState(() {
            widget.daysOfWeek = value;
          });
        },
      ),
    );
  }
}
