import 'package:absensi_sas/app/constant.dart';
import 'package:absensi_sas/model/approval_type_model.dart';
import 'package:absensi_sas/provider/approval_filter_provider.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class ApprovalFilterChipWidget extends HookConsumerWidget {
  const ApprovalFilterChipWidget({
    super.key,
    required this.data,
  });
  final ApprovalTypeModel data;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final currentApprovalFilter = ref.watch(approvalFilterProvider);
    handleOnTap(String value) {
      var tmp = currentApprovalFilter.selectedApprovalType.toList();
      var cek = tmp.contains(value);
      if (cek) {
        tmp.remove(value);
      } else {
        tmp.add(value);
      }
      ref.read(approvalFilterProvider.notifier).state = ApprovalFilterProvider(
          endDate: currentApprovalFilter.endDate,
          keyword: currentApprovalFilter.keyword,
          selectedApprovalType: tmp,
          startDate: currentApprovalFilter.startDate);
    }

    return Container(
      margin: EdgeInsets.only(
          right: Constant.getActualXPhone(context: context, x: 16)),
      child: ElevatedButton(
        onPressed: () {
          handleOnTap(data.value!);
        },
        style: ButtonStyle(
            elevation: MaterialStatePropertyAll(0),
            shape: MaterialStatePropertyAll(RoundedRectangleBorder(
                side: BorderSide(
                  color: currentApprovalFilter.selectedApprovalType
                          .contains(data.value)
                      ? Constant.textOrange
                      : Colors.grey.shade300,
                ),
                borderRadius: BorderRadius.circular(16))),
            backgroundColor: MaterialStateProperty.resolveWith((states) {
              if (currentApprovalFilter.selectedApprovalType
                      .contains(data.value) ==
                  true) {
                return Constant.textOrange.withOpacity(0.1);
              }
              return Colors.white;
            }),
            overlayColor:
                MaterialStatePropertyAll(Constant.textOrange.withOpacity(0.48)),
            foregroundColor: MaterialStatePropertyAll(Colors.white),
            shadowColor: MaterialStatePropertyAll(Colors.white),
            surfaceTintColor: MaterialStatePropertyAll(Colors.white)),
        child: Text(data.name ?? "",
            style: Constant.subtitle_600_14(context: context).copyWith(
                color: currentApprovalFilter.selectedApprovalType
                        .contains(data.value)
                    ? Constant.textOrange
                    : Colors.black)),
      ),
    );
  }
}
