import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/visits/visits_search.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 'package:test_sa/views/widgets/date_and_time/from_to_date_bar.dart';
import 'package:test_sa/views/widgets/equipment/auto_complete_models_field.dart';
import 'package:test_sa/views/widgets/hospitals/hospital_auto_complete_field.dart';
import 'package:test_sa/views/widgets/status/employee/assigned_to_mune.dart';
import 'package:test_sa/views/widgets/status/pentry/pentry_visit_status_mune.dart';
import 'package:test_sa/views/widgets/hospitals/hospital_auto_complete_field.dart';
import 'package:test_sa/views/widgets/titles/app_sub_title.dart';

import '../app_text_form_field.dart';

class VisitsSearchDialog extends StatefulWidget {
  final VisitsSearch initialSearchValue;
  final bool expandedSearch;
  final Function(VisitsSearch) onSearch;

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

class _VisitsSearchDialogState extends State<VisitsSearchDialog> with TickerProviderStateMixin {
  VisitsSearch _search;
  List<Lookup> status = [
    Lookup(
      name: "Done",
      id: 0,
    ),
    Lookup(name: "Not Yet", id: 1),
    Lookup(
      name: "On Hold",
      id: 2,
    ),
  ];

  List<Lookup> contactStatus = [
    // Lookup(name: "Hospital Employee", value: "H",),
    // Lookup(name: "Under Warranty", value: "CW"),
    // Lookup(name: "Under Maintenance Contract", value: "CC",),
  ];

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

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

  @override
  Widget build(BuildContext context) {
    Subtitle _subtitle = AppLocalization.of(context).subtitle;
    DateTime today = DateTime.now();
    return SizedBox(
      height: MediaQuery.of(context).size.height / 1.3,
      child: Form(
        key: _formKey,
        child: ListView(
          // shrinkWrap: true,
          // physics: const ClampingScrollPhysics(),
          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);
                  },
                )
              ],
            ),
            ATextFormField(
              initialValue: _search.deviceSerialNumber,
              hintText: _subtitle.serialNumber,
              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.deviceSerialNumber = value;
              },
            ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            HospitalAutoCompleteField(
              initialValue: _search.hospital?.name,
              // onSave: (value){
              //   _search.hospital = value;
              // },
              onSearch: (value) {
                _search.hospital = value;
                Navigator.of(context).pop(_search);
              },
            ),
            // SizedBox(height: 8.0 * AppStyle.getScaleFactor(context),),
            // ATextFormField(
            //   initialValue: _search.brand,
            //   hintText: _subtitle.brand,
            //   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.brand = value;
            //   },
            // ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            AutoCompleteModelField(
              initialValue: _search.model,
              onPick: (lookup) {
                _search.model = lookup;
                Navigator.of(context).pop(_search);
              },
            ),
            // ATextFormField(
            //   initialValue: _search.model,
            //   hintText: _subtitle.model,
            //   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.model = value;
            //   },
            // ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ASubTitle(_subtitle.status),
            SizedBox(
              height: 4.0 * AppStyle.getScaleFactor(context),
            ),
            PentryVisitsStatusMenu(
              initialValue: _search.statusValue,
              onSelect: (status) {
                _search.statusValue = status;
              },
            ),
            // Wrap(
            //   spacing: 10,
            //   runSpacing: 10,
            //   children: List.generate(
            //       status.length,
            //           (index) {
            //         bool isSelected = _search.statusValue == status[index].id;
            //         return FilterItem(
            //           isSelected: isSelected,
            //           onSelected: (){
            //             if(isSelected) {
            //               _search.statusValue = null;
            //             } else {
            //               _search.statusValue = status[index].id;
            //             }
            //
            //             setState(() {});
            //           },
            //           status: status[index],
            //         );
            //       }
            //
            //   ),
            // ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ASubTitle(_subtitle.contactStatus),
            SizedBox(
              height: 4.0 * AppStyle.getScaleFactor(context),
            ),
            AssignedToMenu(
              initialValue: _search.contactStatus,
              onSelect: (status) {
                _search.contactStatus = status;
              },
            ),
            // Wrap(
            //   spacing: 10,
            //   runSpacing: 10,
            //   children: List.generate(
            //       contactStatus.length,
            //           (index) {
            //         bool isSelected = _search.contactStatus == contactStatus[index];
            //         return FilterItem(
            //           isSelected: isSelected,
            //           onSelected: (){
            //             if(isSelected) {
            //               _search.contactStatus = null;
            //             } else {
            //               _search.contactStatus = contactStatus[index];
            //             }
            //
            //             setState(() {});
            //           },
            //           status: contactStatus[index],
            //         );
            //       }
            //
            //   ),
            // ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ASubTitle(_subtitle.actualDate),
            SizedBox(
              height: 4.0 * AppStyle.getScaleFactor(context),
            ),
            FromToDateBar(
              from: _search.actualDateFrom,
              to: _search.actualDateTo,
              onPickFrom: (date) {
                _search.actualDateFrom = date;
              },
              onPickTo: (date) {
                _search.actualDateTo = date;
              },
            ),
            SizedBox(
              height: 8.0 * AppStyle.getScaleFactor(context),
            ),
            ASubTitle(_subtitle.expectDate),
            SizedBox(
              height: 4.0 * AppStyle.getScaleFactor(context),
            ),
            FromToDateBar(
              from: _search.expectedDateFrom ?? DateTime(today.year, today.month, 1),
              to: _search.expectedDateTo ?? DateTime(today.year, (today.month + 1).clamp(1, 12), today.month == 12 ? 31 : 0),
              onPickFrom: (date) {
                _search.expectedDateFrom = date;
              },
              onPickTo: (date) {
                _search.expectedDateTo = date;
              },
            ),
            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 = VisitsSearch();
                    Navigator.of(context).pop(_search);
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
