import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../cubit/menu_history_cubit.dart';
import '../../../cubit/tab_history_cubit.dart';
import '../../../utils/U.dart';
import '../../../utils/extensions.dart';

class BuildFilter extends StatelessWidget {
  const BuildFilter({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MenuHistoryCubit, MenuHistoryState>(
        builder: (context, state) {
          HistoryTab tab = state.activeTab;
          List currentFilters = tab.filters;

          return SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: tab == HistoryTab.ongoing
                ? _ongoingFilter(currentFilters)
                : _doneFilter(currentFilters),
          );
        }
    );
  }

  _ongoingFilter(List<dynamic> currentFilters) {
    return BlocBuilder<OngoingCubit, TabState>(
      builder: (context, state) {
        return Row(
          children: List.generate(currentFilters.length, (i) {
            return GestureDetector(
              child: _filterItem(state, currentFilters, i),
              onTap: () {
                String currentFilter = '';

                if (state.filter != currentFilters[i]['value']) {
                  currentFilter = currentFilters[i]['value'];
                } else {
                  currentFilter = 'none';
                }
                context.read<OngoingCubit>().setFilter(currentFilter);
                // context.read<OngoingCubit>().loadOngoing();
              },
            ).withHover();
          }),
        );
      }
    );
  }

  _doneFilter(List<dynamic> currentFilters) {
    return BlocBuilder<DoneCubit, TabState>(
      builder: (context, state) {
        return Row(
          children: List.generate(currentFilters.length, (i) {
            return GestureDetector(
              child: _filterItem(state, currentFilters, i),
              onTap: () {
                String currentFilter = '';

                if (state.filter != currentFilters[i]['value']) {
                  currentFilter = currentFilters[i]['value'];
                } else {
                  currentFilter = 'none';
                }
                context.read<DoneCubit>().setFilter(currentFilter);
                // context.read<DoneCubit>().loadDone();
              },
            ).withHover();
          }),
        );
      }
    );
  }

  Widget? _filterItem(TabState state, List<dynamic> currentFilters, int i) {
    return Container(
      margin: EdgeInsets.only(left: 16),
      padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
      decoration: BoxDecoration(
          color: state.filter == currentFilters[i]['value']
              ? primaryColor
              : Color(0xffF2F8F7),
          border: Border.all(
              color: state.filter == currentFilters[i]['value']
                  ? primaryColor
                  : Color(0xffBEC1C1)),
          borderRadius: BorderRadius.all(Radius.circular(50))),
      child: Text(currentFilters[i]['title'], style: TextStyle(
          color: state.filter == currentFilters[i]['value'] ? Colors
              .white : textColor, fontSize: 16)),
    );
  }
}

class FilterItem extends StatelessWidget {
  final TabState state;
  final List<dynamic> currentFilters;
  final int i;

  const FilterItem({super.key, required this.state, required this.currentFilters, required this.i});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 16),
      padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
      decoration: BoxDecoration(
          color: state.filter == currentFilters[i]['value']
              ? primaryColor
              : Color(0xffF2F8F7),
          border: Border.all(
              color: state.filter == currentFilters[i]['value']
                  ? primaryColor
                  : Color(0xffBEC1C1)),
          borderRadius: BorderRadius.all(Radius.circular(50))),
      child: Text(currentFilters[i]['title'], style: TextStyle(
          color: state.filter == currentFilters[i]['value'] ? Colors
              .white : textColor, fontSize: 16)),
    );
  }
}

class OngoingFilter extends StatelessWidget {
  final List<dynamic> currentFilters;

  const OngoingFilter({super.key, this.currentFilters = const []});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<OngoingCubit, TabState>(
        builder: (context, state) {
          return Row(
            children: List.generate(currentFilters.length, (i) {
              return GestureDetector(
                child: FilterItem(state: state, currentFilters: currentFilters, i: i),
                onTap: () {
                  String currentFilter = '';

                  if (state.filter != currentFilters[i]['value']) {
                    currentFilter = currentFilters[i]['value'];
                  } else {
                    currentFilter = 'none';
                  }
                  context.read<OngoingCubit>().setFilter(currentFilter);
                  // context.read<OngoingCubit>().loadOngoing();
                },
              ).withHover();
            }),
          );
        }
    );
  }
}
