import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/models/patient/patient_model.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app_text_form_field.dart';
import 'package:doctor_app_flutter/widgets/shared/user-guid/custom_validation_error.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:intl/intl.dart';

class DynamicElements extends StatefulWidget {
  final PatientModel _patientSearchFormValues;
  final bool isFormSubmitted;
  DynamicElements(this._patientSearchFormValues, this.isFormSubmitted);
  @override
  _DynamicElementsState createState() => _DynamicElementsState();
}

class _DynamicElementsState extends State<DynamicElements> {
  TextEditingController _toDateController = new TextEditingController();
  TextEditingController _fromDateController = new TextEditingController();
  void _presentDatePicker(id) {
    showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2019),
      lastDate: DateTime.now(),
    ).then((pickedDate) {
      if (pickedDate == null) {
        return;
      }
      setState(() {
        print(id);
        var selectedDate = DateFormat.yMd().format(pickedDate);

        if (id == '_selectedFromDate') {
          //    _fromDateController.text = selectedDate;
          selectedDate = pickedDate.year.toString() +
              "-" +
              pickedDate.month.toString().padLeft(2, '0') +
              "-" +
              pickedDate.day.toString().padLeft(2, '0');

          _fromDateController.text = selectedDate;
        } else {
          selectedDate = pickedDate.year.toString() +
              "-" +
              pickedDate.month.toString().padLeft(2, '0') +
              "-" +
              pickedDate.day.toString().padLeft(2, '0');

          _toDateController.text = selectedDate;
          //    _toDateController.text = selectedDate;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    InputDecoration textFieldSelectorDecoration(
        {String hintText,
        String selectedText,
        bool isDropDown,
        IconData icon}) {
      return InputDecoration(
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
          borderRadius: BorderRadius.circular(8),
        ),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
          borderRadius: BorderRadius.circular(8),
        ),
        disabledBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
          borderRadius: BorderRadius.circular(8),
        ),
        hintText: selectedText != null ? selectedText : hintText,
        suffixIcon: isDropDown ? Icon(icon ?? Icons.arrow_drop_down) : null,
        hintStyle: TextStyle(
          fontSize: 14,
          color: Colors.grey.shade600,
        ),
      );
    }

    return LayoutBuilder(
      builder: (ctx, constraints) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(
              height: 10,
            ),
            SizedBox(
              height: 10,
            ),
            Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(6.0)),
                    border: Border.all(width: 1.0, color: HexColor("#CCCCCC"))),
                padding: EdgeInsets.all(10),
                child: AppTextFormField(
                  borderColor: Colors.white,
                  onTap: () => _presentDatePicker('_selectedFromDate'),
                  hintText: TranslationBase.of(context).fromDate,
                  controller: _fromDateController,
                  inputFormatter: ONLY_DATE,
                  onSaved: (value) {
                    if (_fromDateController.text.toString().trim().isEmpty) {
                      widget._patientSearchFormValues.From = "0";
                    } else {
                      widget._patientSearchFormValues.From =
                          _fromDateController.text.replaceAll("/", "-");
                    }
                  },
                  readOnly: true,
                )),
            SizedBox(
              height: 5,
            ),
            if (widget._patientSearchFormValues.From == "0" &&
                widget.isFormSubmitted)
              CustomValidationError(),
            SizedBox(
              height: 10,
            ),
            Container(
                decoration: BoxDecoration(
                    border: Border.all(width: 1.0, color: HexColor("#CCCCCC")),
                    borderRadius: BorderRadius.all(Radius.circular(6.0))),
                padding: EdgeInsets.all(10),
                child: AppTextFormField(
                  readOnly: true,
                  borderColor: Colors.white,
                  hintText: TranslationBase.of(context).toDate,
                  controller: _toDateController,
                  onTap: () {
                    _presentDatePicker('_selectedToDate');
                  },
                  inputFormatter: ONLY_DATE,
                  onSaved: (value) {
                    if (_toDateController.text.toString().trim().isEmpty) {
                      widget._patientSearchFormValues.To = "0";
                    } else {
                      widget._patientSearchFormValues.To =
                          _toDateController.text.replaceAll("/", "-");
                    }
                  },
                )),
            if (widget._patientSearchFormValues.To == "0" &&
                widget.isFormSubmitted)
              CustomValidationError(),
            SizedBox(
              height: 10,
            ),
          ],
        );
      },
    );
  }
}
