import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/gen/assets.gen.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/features/invoice/data/models/invoice_model.dart';
import 'package:krow/features/invoice/domain/blocs/invoice_details_bloc/invoice_details_bloc.dart';
import 'package:krow/features/invoice/domain/invoice_entity.dart';

class InvoiceFromToWidget extends StatefulWidget {
  final InvoiceModel item;

  const InvoiceFromToWidget({super.key, required this.item});

  @override
  State<InvoiceFromToWidget> createState() => _InvoiceFromToWidgetState();
}

class _InvoiceFromToWidgetState extends State<InvoiceFromToWidget> {
  ExpandableController? _expandableFromController;
  ExpandableController? _expandableToController;

  @override
  void initState() {
    super.initState();
    _expandableFromController = ExpandableController(initialExpanded: true);
    _expandableToController = ExpandableController(initialExpanded: true);
  }

  @override
  void dispose() {
    super.dispose();
    _expandableFromController?.dispose();
    _expandableToController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<InvoiceDetailsBloc, InvoiceDetailsState>(
      builder: (context, state) {
        return ExpandableTheme(
          data: ExpandableThemeData(
            headerAlignment: ExpandablePanelHeaderAlignment.center,
            iconPadding: const EdgeInsets.only(left: 12),
          ),
          child: Container(
            decoration: KwBoxDecorations.white12,
            padding: const EdgeInsets.symmetric(horizontal: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Gap(12),
                ExpandablePanel(
                  collapsed: Container(),
                  controller: _expandableFromController,
                  expanded: _fromInfo(),
                  header: _buildHeader(
                      context, _expandableFromController, 'From: Legendary'),
                ),
                ExpandablePanel(
                  collapsed: Container(),
                  controller: _expandableToController,
                  expanded: _toInfo(state.invoiceModel!),
                  header: _buildHeader(
                      context, _expandableToController, 'To: ${state.invoiceModel?.business?.name}'),
                ),
                Gap(12),

              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildHeader(BuildContext context, ExpandableController? controller,
      String title,) {
    return GestureDetector(
      onTap: () {
        controller?.toggle();
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 12),
        color: Colors.transparent,
        child: Text(
          title,
          style: AppTextStyles.headingH3,
        ),
      ),
    );
  }

  Widget _fromInfo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Gap(4),
        infoElement('Address', '848 E Gish Rd, Suite 1 San Jose, CA 95112'),
        Gap(12),
        infoElement('Phone Number', '4088360180'),
        Gap(12),
        infoElement('Email', 'orders@legendaryeventstaff.com'),
        Gap(20),
      ],
    );
  }

  Widget _toInfo(InvoiceModel invoice) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Gap(4),
        infoElement('Hub', invoice.event?.hub?.name ?? ''),
        Gap(12),
        infoElement('Address', invoice.business?.registration ?? ''),
        Gap(12),
        infoElement('Full Name', '${invoice.business?.contact?.firstName??''} ${invoice.business?.contact?.lastName??''}' ),
        Gap(12),
        infoElement('Phone Number', invoice.business?.contact?.phone ?? ''),
        Gap(12),
        infoElement('Email', invoice.business?.contact?.email ?? ''),
        Gap(12),
      ],
    );
  }

  Widget infoElement(String title, String value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style:
          AppTextStyles.bodySmallReg.copyWith(color: AppColors.blackGray),
        ),
        Gap(2),
        Text(
          value,
          style: AppTextStyles.bodyMediumMed,
        )
      ],
    );
  }
}
