import 'package:flutter/material.dart';

import '../../../controllers/localization/localization.dart';
import '../../../models/lookup.dart';
import '../../../models/service_request/service_request_search.dart';
import '../../../models/subtitle.dart';
import '../../app_style/sizing.dart';
import '../app_text_form_field.dart';
import '../buttons/app_button.dart';
import '../buttons/app_small_button.dart';
import '../hospitals/hospital_auto_complete_field.dart';
import 'filter_item.dart';

class ServiceRequestsSearchDialog extends StatefulWidget {
  final ServiceRequestSearch initialSearchValue;
  final bool? expandedSearch;
  final Function(ServiceRequestSearch)? onSearch;

  const ServiceRequestsSearchDialog({
    Key? key,
    required this.initialSearchValue,
    this.expandedSearch,
    this.onSearch,
  }) : super(key: key);

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

class ServiceRequestsSearchDialogState extends State<ServiceRequestsSearchDialog> with TickerProviderStateMixin {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  late ServiceRequestSearch _search;
  late List<Lookup> status = const [
    Lookup(label: "New", id: 4),
    Lookup(label: "Repaired", id: 6),
    Lookup(label: "Repeated", id: 8),
    Lookup(label: "Closed", id: 9),
    Lookup(label: "Under Repair", id: 5),
  ];

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

  @override
  Widget build(BuildContext context) {
    Subtitle? subtitle = AppLocalization.of(context)?.subtitle;
    return SizedBox(
      height: MediaQuery.of(context).size.height / 1.2,
      child: Form(
        key: _formKey,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ASmallButton(
                    text: subtitle?.cancel,
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  ASmallButton(
                    text: subtitle?.search,
                    onPressed: () {
                      if (!(_formKey.currentState?.validate() ?? true)) return;
                      _formKey.currentState?.save();
                      Navigator.of(context).pop(_search);
                    },
                  )
                ],
              ),
              SizedBox(height: 8.0 * AppStyle.getScaleFactor(context)),
              ATextFormField(
                initialValue: _search.deviceSerialNumber,
                hintText: subtitle?.serialNumber,
                style: Theme.of(context).textTheme.titleLarge,
                textInputAction: TextInputAction.search,
                onAction: () {
                  if (!(_formKey.currentState?.validate() ?? true)) 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 ?? '',
                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.deviceName,
                hintText: subtitle?.deviceName,
                style: Theme.of(context).textTheme.titleLarge,
                textInputAction: TextInputAction.search,
                onAction: () {
                  if (!(_formKey.currentState?.validate() ?? true)) return;
                  _formKey.currentState?.save();
                  Navigator.of(context).pop(_search);
                },
                onSaved: (value) {
                  _search.deviceName = value;
                },
              ),
              SizedBox(height: 8.0 * AppStyle.getScaleFactor(context)),
              ATextFormField(
                initialValue: _search.model,
                hintText: subtitle?.model,
                style: Theme.of(context).textTheme.titleLarge,
                textInputAction: TextInputAction.search,
                onAction: () {
                  if (!(_formKey.currentState?.validate() ?? true)) return;
                  _formKey.currentState?.save();
                  Navigator.of(context).pop(_search);
                },
                onSaved: (value) {
                  _search.model = value;
                },
              ),
              SizedBox(
                height: 16 * AppStyle.getScaleFactor(context),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12),
                child: Wrap(
                  spacing: 10,
                  runSpacing: 10,
                  alignment: WrapAlignment.spaceEvenly,
                  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],
                    );
                  }),
                ),
              ),
              Visibility(
                visible: widget.initialSearchValue.toSearchString().isNotEmpty,
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    vertical: 8,
                    horizontal: 16,
                  ),
                  child: AButton(
                    padding: EdgeInsets.zero,
                    text: subtitle?.clearSearch ?? '',
                    onPressed: () {
                      _search = ServiceRequestSearch();
                      Navigator.of(context).pop(_search);
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
