import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/data/models/event/hub_model.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_dropdown.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/create_event/domain/bloc/create_event_bloc.dart';
import 'package:krow/features/create_event/presentation/event_date_section/event_date_input_widget.dart';

import '../../../../core/entity/event_entity.dart';

class CreateEventDetailsCardWidget extends StatefulWidget {
  const CreateEventDetailsCardWidget({super.key});

  @override
  State<CreateEventDetailsCardWidget> createState() =>
      _CreateEventDetailsCardWidgetState();
}

class _CreateEventDetailsCardWidgetState
    extends State<CreateEventDetailsCardWidget>
    with AutomaticKeepAliveClientMixin {
  TextEditingController poNumberController = TextEditingController();
  // TextEditingController contractNumberController = TextEditingController();
  final TextEditingController _nameController = TextEditingController();

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BlocConsumer<CreateEventBloc, CreateEventState>(
      listener: (context, state) {
        if (state.entity.name != _nameController.text) {
          _nameController.text = state.entity.name;
        }

        // if (state.entity.contractNumber != null &&
        //     state.entity.contractNumber != contractNumberController.text) {
        //   contractNumberController.text = state.entity.contractNumber!;
        // }
        //
        if (state.entity.poNumber != null &&
            state.entity.poNumber != poNumberController.text) {
          poNumberController.text = state.entity.poNumber!;
        }
      },
      builder: (context, state) {
        return AnimatedContainer(
          duration: Duration(milliseconds: 300),
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 24),
          decoration: KwBoxDecorations.white12.copyWith(
              border: state.validationState != null
                  ? Border.all(color: AppColors.statusError, width: 1)
                  : null),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                'Event Details',
                style: AppTextStyles.bodyMediumMed,
              ),
              const Gap(12),
              KwTextInput(
                controller: _nameController,
                title: 'Event Name',
                hintText: 'Enter event name',
                onChanged: (value) {
                  BlocProvider.of<CreateEventBloc>(context)
                      .add(CreateEventNameChange(value));
                },
              ),
              IgnorePointer(
                  ignoring: ![EventStatus.draft, EventStatus.pending].contains(state.entity.status) && state.entity.status!=null,
                  child: EventDateInputWidget(state.entity)),
              const Gap(8),
              _hubDropdown(state.hubs, state),
              const Gap(8),
              // _contractDropdown(context, state),
              // if (state.entity.contractType == EventContractType.contract)
              //   _buildContractInput(),
              // if (state.entity.contractType == EventContractType.purchaseOrder)
                _buildPurchaseInput(),
            ],
          ),
        );
      },
    );
  }

  Column _hubDropdown(List<HubModel> hubs, CreateEventState state) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 16),
          child: Text(
            'Location',
            style:
                AppTextStyles.bodyTinyReg.copyWith(color: AppColors.blackGray),
          ),
        ),
        const Gap(4),
        KwDropdown(
            horizontalPadding: 28,
            hintText: 'Hub name',
            selectedItem: state.entity.hub != null
                ? KwDropDownItem(
                    data: state.entity.hub, title: state.entity.hub?.name ?? '')
                : null,
            items: hubs
                .map((e) => KwDropDownItem(data: e, title: e.name ?? ''))
                .toList(),
            onSelected: (item) {
              BlocProvider.of<CreateEventBloc>(context)
                  .add(CreateEventChangeHub(item!));
            }),
      ],
    );
  }

//   Column _contractDropdown(BuildContext context, CreateEventState state) {
//     return Column(
//       crossAxisAlignment: CrossAxisAlignment.start,
//       children: [
//         Padding(
//           padding: const EdgeInsets.only(left: 16),
//           child: Text(
//             'Payment type',
//             style:
//                 AppTextStyles.bodyTinyReg.copyWith(color: AppColors.blackGray),
//           ),
//         ),
//         const Gap(4),
//         KwDropdown(
//             horizontalPadding: 28,
//             hintText: 'Direct',
//             selectedItem: KwDropDownItem(
//                 data: state.entity.contractType,
//                 title: state.entity.contractType.formattedName),
//             items: const [
//               KwDropDownItem(data: EventContractType.direct, title: 'Direct'),
//               KwDropDownItem(
//                   data: EventContractType.purchaseOrder,
//                   title: 'Purchase Order'),
//               KwDropDownItem(
//                   data: EventContractType.contract, title: 'Contract'),
//             ],
//             onSelected: (item) {
//               BlocProvider.of<CreateEventBloc>(context)
//                   .add(CreateEventChangeContractType(item));
//             }),
//       ],
//     );
//   }
//
//   _buildContractInput() {
//     return Padding(
//       padding: const EdgeInsets.only(top:8.0),
//       child: KwTextInput(
//           controller: contractNumberController,
//           onChanged: (value) {
//             BlocProvider.of<CreateEventBloc>(context)
//                 .add(CreateEventChangeContractNumber(value));
//           },
//           title: 'Contract number',
//           hintText: '#00000'),
//     );
//   }
//
  _buildPurchaseInput() {
    return Padding(
      padding: const EdgeInsets.only(top:8.0),
      child: KwTextInput(
          controller: poNumberController,
          onChanged: (value) {
            BlocProvider.of<CreateEventBloc>(context)
                .add(CreateEventChangePoNumber(value));
          },
          title: 'PO Reference number',
          hintText: 'PO Reference number'),
    );
  }
}
//
// extension on EventContractType? {
//   String get formattedName {
//     return switch (this) {
//       EventContractType.direct => 'Direct',
//       EventContractType.contract => 'Contract',
//       EventContractType.purchaseOrder => 'Purchase Order',
//       null => 'null'
//     };
//   }
// }
