import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.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/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:intl/intl.dart';
import 'package:jiffy/jiffy.dart';

import 'ovulation_result_page.dart';

class OvulationPeriod extends StatefulWidget {
  @override
  _OvulationPeriodState createState() => _OvulationPeriodState();
}

class _OvulationPeriodState extends State<OvulationPeriod> {
  DateTime bloodSugarDate = DateTime.now();
  DateTime timeSugarDate = DateTime.now();
  DateTime selectedDateTime = DateTime.now();

  // int cycleLength = 0;
  // int lutealPhaseLength = 0;
  String selectedDate;
  var dateFrom = DateTime.now();
  var babyAgeWeeks;
  var babyAgeDays;
  var dateTo = DateTime.now();
  var conceivedDate = DateTime.now();
  var deliveryDue = DateTime.now();
  var dt = DateTime.now();
  var newFormat = DateFormat("yy-MM-dd");
  String updatedDt;

  TextEditingController cycleLengthController = new TextEditingController();
  TextEditingController lutelLengthController = new TextEditingController();

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

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

  calculateBabyInformation() {
    setState(() {
      bloodSugarDate = selectedDateTime;
      dateFrom = selectedDateTime.add(Duration(days: 10));
      updatedDt = DateFormat.yMMMEd().format(dateFrom);
      dateTo = selectedDateTime.add(Duration(days: 20));
      conceivedDate = selectedDateTime.add(Duration(days: 14));
      deliveryDue = selectedDateTime.add(Duration(days: 280));

      // babyAge = Jiffy([DateTime.now()]).diff(Jiffy([date]), Units.WEEK);
      babyAgeWeeks = Jiffy([DateTime.now().year, DateTime.now().month, DateTime.now().day]).diff(Jiffy([selectedDateTime.year, selectedDateTime.month, dateTo.day]), Units.WEEK);
      babyAgeDays = Jiffy([DateTime.now().year, DateTime.now().month, DateTime.now().day]).diff(Jiffy([selectedDateTime.year, selectedDateTime.month, dateTo.day]), Units.DAY);
    });
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      isShowDecPage: false,
      appBarTitle: TranslationBase.of(context).ovulation,
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.all(21),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      TranslationBase.of(context).ovulationDesc,
                      style: TextStyle(
                        fontSize: 14,
                        letterSpacing: -0.54,
                        color: Colors.black,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    SizedBox(
                      height: 12.0,
                    ),
                    InkWell(
                      onTap: () {
                        DatePicker.showDatePicker(
                          context,
                          showTitleActions: true,
                          minTime: DateTime(DateTime.now().year - 1, 1, 1),
                          maxTime: DateTime.now(),
                          onConfirm: (date) {
                            print('confirm $date');
                            setState(() {
                              selectedDateTime = date;
                            });
                          },
                          currentTime: DateTime.now(),
                        );
                      },
                      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(TranslationBase.of(context).date),
                            Texts(getDate()),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 12.0,
                    ),
                    inputWidget(TranslationBase.of(context).cycleLabel, "0", cycleLengthController),
                    SizedBox(
                      height: 12.0,
                    ),
                    inputWidget(TranslationBase.of(context).lutealLabel, "0", lutelLengthController),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
            color: Colors.white,
            child: SecondaryButton(
              label: TranslationBase.of(context).calculate,
              color: CustomColors.accentColor,
              onTap: () {
                calculateBabyInformation();
                Navigator.push(
                  context,
                  FadePage(
                    page: OvulationResult(
                      conceivedDate: conceivedDate,
                      dateFrom: dateFrom,
                      dateTo: dateTo,
                      deliveryDue: deliveryDue,
                      babyAge: babyAgeWeeks,
                      babyAgeDays: babyAgeDays,
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  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(15),
        color: Colors.white,
        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,
                      hintStyle: TextStyle(
                        fontSize: 14,
                        height: 21 / 14,
                        fontWeight: FontWeight.w400,
                        color: Color(0xff575757),
                        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),
          ],
        ),
      ),
    );
  }
}
