import 'package:farm_tpf/utils/validators.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker_plus/flutter_datetime_picker_plus.dart' as datatTimePicker;
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:farm_tpf/utils/formatter.dart';

import 'widget_field_time_picker.dart';

class FieldDateWidget extends StatefulWidget {
  final String hint;
  final String? value;
  final Function(DateTime) onPressed;
  final String tag;
  final String? Function(String?)? validator;
  final bool? isValidated;
  FieldDateWidget({
    required this.hint,
    required this.tag,
    this.value,
    required this.onPressed,
    this.validator,
    this.isValidated,
  });

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

class _FieldDateWidgetState extends State<FieldDateWidget> {
  ChangeDateTimePicker? controller;
  final textController = TextEditingController();

  @override
  void initState() {
    super.initState();
    controller = Get.put(ChangeDateTimePicker(), tag: widget.tag);
    textController.addListener(() {
      print('object');
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChangeDateTimePicker>(
        init: controller,
        tag: widget.tag,
        builder: (data) {
          return SizedBox(
            width: double.infinity,
            height: isShowError() ? 85 : 65,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                isShowError()
                    ? Text(
                        widget.hint ?? '',
                        style: TextStyle(color: Colors.red, fontSize: 13.0),
                      )
                    : Text(
                        '',
                        style: TextStyle(color: Colors.black54, fontSize: 13.0),
                      ),
                SizedBox(
                  width: double.infinity,
                  height: 44,
                  child: TextButton(
                      onPressed: () {
                        datatTimePicker.DatePicker.showDateTimePicker(
                          context,
                          showTitleActions: true,
                          onChanged: (date) {},
                          onConfirm: (date) {
                            controller?.change(date);
                            widget.onPressed(date);
                            textController.text = date.toString();
                          },
                          currentTime: data?.selectedDateTime,
                          locale: datatTimePicker.LocaleType.vi,
                        );
                      },
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                              padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
                              decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                  width: 0.5,
                                  color: isShowError() ? Colors.red : Colors.black54,
                                )),
                              ),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Validators.stringNotNullOrEmpty(data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm() ?? '')
                                        ? Text(data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm() ?? '',
                                            style: TextStyle(fontSize: 16.0, color: Colors.black))
                                        : Text(widget.hint, style: TextStyle(fontSize: 16.0, color: Colors.black54)),
                                  ),
                                  Icon(
                                    Icons.date_range,
                                    color: Colors.grey,
                                  ),
                                ],
                              ))
                        ],
                      )),
                ),
                isShowError()
                    ? Text(
                        (widget.isValidated ?? false) ? 'Vui lòng nhập ${widget.hint}' : '',
                        style: TextStyle(fontSize: 12.0, color: Colors.red, fontWeight: FontWeight.normal),
                        textAlign: TextAlign.left,
                      )
                    : SizedBox(),
                Container(
                  height: 0,
                  child: TextFormField(
                    style: TextStyle(color: Colors.white),
                    decoration: InputDecoration(border: InputBorder.none, labelStyle: TextStyle(color: Colors.white)),
                    validator: widget.validator,
                    controller: textController,
                  ),
                )
              ],
            ),
          );
        });
  }

  bool isShowError() {
    if (widget.validator == null) {
      return false;
    } else {
      if (widget.isValidated ?? false) {
        return Validators.stringNotNullOrEmpty(textController.text) ? false : true;
      } else {
        return false;
      }
    }
  }
}
