import 'package:flutter/material.dart';
import 'package:test_sa/models/part.dart';
import 'package:test_sa/models/service_request/search_work_order.dart';
import 'package:test_sa/views/app_style/colors.dart';

import '../../../controllers/localization/localization.dart';
import '../../../models/subtitle.dart';
import '../../app_style/sizing.dart';
import '../../widgets/buttons/app_button.dart';
import '../../widgets/parts/auto_complete_parts_field.dart';
import '../../widgets/parts/part_item.dart';
import '../../widgets/titles/app_sub_title.dart';

class SparePartsBottomSheet extends StatefulWidget {
  final SearchWorkOrder subWorkOrder;
  const SparePartsBottomSheet({this.subWorkOrder, Key key}) : super(key: key);

  @override
  State<SparePartsBottomSheet> createState() => _SparePartsBottomSheetState();
}

class _SparePartsBottomSheetState extends State<SparePartsBottomSheet> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final SearchWorkOrder _workOrder = SearchWorkOrder();
  bool _validate = false;

  @override
  void initState() {
    super.initState();
    _workOrder.copyFrom(widget.subWorkOrder);
  }

  @override
  void dispose() {
    _workOrder.sparePartsWorkOrders = widget.subWorkOrder.sparePartsWorkOrders;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final Subtitle subtitle = AppLocalization.of(context).subtitle;
    return Padding(
      padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
      child: ClipRRect(
        borderRadius: const BorderRadius.only(
          topLeft: Radius.circular(15),
          topRight: Radius.circular(15),
        ),
        clipBehavior: Clip.antiAliasWithSaveLayer,
        child: Container(
          color: Colors.white,
          height: size.height * 0.9,
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
          child: Form(
            key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const ASubTitle("Spare Parts"),
                const SizedBox(height: 8),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            width: double.infinity,
                            padding: const EdgeInsets.all(16),
                            margin: const EdgeInsets.symmetric(vertical: 16),
                            decoration: BoxDecoration(color: AColors.grey, borderRadius: BorderRadius.circular(AppStyle.getBorderRadius(context)), boxShadow: const [
                              BoxShadow(
                                color: AColors.grey,
                                offset: Offset(0, -1),
                              )
                            ]),
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Row(
                                  children: [
                                    Expanded(
                                      flex: 3,
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          ASubTitle(subtitle.partNumber),
                                          _validate && _workOrder.sparePartsWorkOrders == null
                                              ? ASubTitle(
                                                  subtitle.requiredWord,
                                                  color: Colors.red,
                                                )
                                              : const SizedBox.shrink(),
                                          const SizedBox(height: 4),
                                          AutoCompletePartsField(
                                            onPick: (part) {
                                              _workOrder.sparePartsWorkOrders ??= [];
                                              _workOrder.sparePartsWorkOrders.add(SparePartsWorkOrders(
                                                id: part.reportPartID,
                                                qty: part.quantity,
                                                sparePart: SparePart(id: part.id, partName: part.name, partNo: part.code),
                                              ));
                                            },
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(
                                  height: 8 * AppStyle.getScaleFactor(context),
                                ),
                                if (_workOrder.sparePartsWorkOrders?.isNotEmpty ?? false)
                                  Row(
                                    children: [
                                      Expanded(flex: 3, child: Text(subtitle.number)),
                                      Expanded(flex: 1, child: Text(subtitle.quantity)),
                                    ],
                                  ),
                                if (_workOrder.sparePartsWorkOrders?.isNotEmpty ?? false)
                                  Column(
                                    children: List.generate(
                                      _workOrder.sparePartsWorkOrders?.length,
                                      (index) {
                                        final spare = _workOrder.sparePartsWorkOrders[index];
                                        Part part = Part(
                                          id: spare.sparePart?.id,
                                          reportPartID: spare.id,
                                          code: spare.sparePart?.partNo,
                                          name: spare.sparePart?.partName,
                                          quantity: spare.qty,
                                        );
                                        return PartItem(
                                          part: part,
                                          onEdit: (qty) {
                                            spare.qty = qty;
                                          },
                                          onDelete: (part) {
                                            _workOrder.sparePartsWorkOrders.remove(spare);
                                            setState(() {});
                                          },
                                        );
                                      },
                                    ),
                                  ),
                                const SizedBox(height: 16),
                              ],
                            ),
                          ),
                          const SizedBox(height: 24),
                        ],
                      ),
                    ),
                  ),
                ),
                AButton(
                  text: subtitle.submit,
                  onPressed: () async {
                    _formKey.currentState.save();
                    widget.subWorkOrder.copyFrom(_workOrder);
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
