import 'package:flutter/material.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/models/device/device_transfer_search.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/buttons/app_button.dart';
import 'package:test_sa/views/widgets/buttons/app_small_button.dart';

import '../app_text_form_field.dart';
import '../date_and_time/date_picker.dart';
import '../status/report/service_report_visit_date_operator.dart';
import '../switch_button.dart';
import '../titles/app_sub_title.dart';

class AssetTransferSearchDialog extends StatefulWidget {
  final DeviceTransferSearch initialSearchValue;
  final bool expandedSearch;
  final Function(DeviceTransferSearch) onSearch;

  const AssetTransferSearchDialog({Key key, this.initialSearchValue, this.expandedSearch, this.onSearch}) : super(key: key);
  @override
  AssetTransferSearchDialogState createState() => AssetTransferSearchDialogState();
}

class AssetTransferSearchDialogState extends State<AssetTransferSearchDialog> with TickerProviderStateMixin {
  DeviceTransferSearch _search;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    _search = DeviceTransferSearch();
    _search.fromSearch(widget.initialSearchValue);
  }

  @override
  Widget build(BuildContext context) {
    Subtitle subtitle = AppLocalization.of(context).subtitle;
    return SizedBox(
      height: MediaQuery.of(context).size.height / 1.3,
      child: Form(
        key: _formKey,
        child: ListView(
          padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                ASmallButton(
                  text: subtitle.cancel,
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                ASmallButton(
                  text: subtitle.search,
                  onPressed: () {
                    if (!_formKey.currentState.validate()) {
                      return;
                    }
                    _formKey.currentState.save();
                    Navigator.of(context).pop(_search);
                  },
                )
              ],
            ),
            ASwitchButton(
              title: "Most Recent",
              value: _search.mostRecent ?? false,
              onChange: (value) {
                _search.mostRecent = value;
                setState(() {});
              },
            ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ATextFormField(
              initialValue: _search.assetNumber,
              hintText: subtitle.assetNumber,
              style: Theme.of(context).textTheme.headline6,
              textInputAction: TextInputAction.search,
              onAction: () {
                if (!_formKey.currentState.validate()) {
                  return;
                }
                _formKey.currentState.save();
                Navigator.of(context).pop(_search);
              },
              onSaved: (value) {
                _search.assetNumber = value;
              },
            ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ATextFormField(
              initialValue: _search.assetName,
              hintText: subtitle.assetName,
              style: Theme.of(context).textTheme.headline6,
              textInputAction: TextInputAction.search,
              onAction: () {
                if (!_formKey.currentState.validate()) {
                  return;
                }
                _formKey.currentState.save();
                Navigator.of(context).pop(_search);
              },
              onSaved: (value) {
                _search.site = value;
              },
            ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ATextFormField(
              initialValue: _search.site,
              hintText: subtitle.site,
              style: Theme.of(context).textTheme.headline6,
              textInputAction: TextInputAction.search,
              onAction: () {
                if (!_formKey.currentState.validate()) {
                  return;
                }
                _formKey.currentState.save();
                Navigator.of(context).pop(_search);
              },
              onSaved: (value) {
                _search.assetName = value;
              },
            ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            const ASubTitle("Request Date"),
            const SizedBox(height: 4),
            ServiceReportVisitDateOperator(
              initialValue: _search.dateOperator,
              onSelect: (status) {
                _search.dateOperator = status;
                setState(() {});
              },
            ),
            Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      if (_search?.dateOperator?.name?.toLowerCase()?.contains("between") ?? false) const ASubTitle("From"),
                      ADatePicker(
                        date: DateTime.tryParse(_search.from ?? ""),
                        from: DateTime(1950),
                        onDatePicker: (date) {
                          _search.from = date?.toIso8601String();
                          setState(() {});
                        },
                      ),
                    ],
                  ),
                ),
                if (_search?.dateOperator?.name?.toLowerCase()?.contains("between") ?? false) const SizedBox(width: 16),
                if (_search?.dateOperator?.name?.toLowerCase()?.contains("between") ?? false)
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        const ASubTitle("To"),
                        ADatePicker(
                          date: DateTime.tryParse(_search.to ?? ""),
                          from: DateTime(1950),
                          onDatePicker: (date) {
                            _search.to = date?.toIso8601String();
                            setState(() {});
                          },
                        ),
                      ],
                    ),
                  ),
              ],
            ),
            const SizedBox(width: 16),
            Visibility(
              visible: _search.toMap().isNotEmpty,
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
                child: AButton(
                  padding: EdgeInsets.zero,
                  text: subtitle.clearSearch,
                  onPressed: () {
                    _search = DeviceTransferSearch();
                    Navigator.of(context).pop(_search);
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
