import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/core/application/di/injectable.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/features/check_list/domain/bloc/check_list_event.dart';
import 'package:krow/features/check_list/domain/bloc/check_list_state.dart';
import 'package:krow/features/check_list/domain/check_list_repository.dart';
import 'package:krow/features/profile/role_kit/domain/staff_role_kit_repository_impl.dart';

class CheckListBloc extends Bloc<CheckListEvent, CheckListState> {
  CheckListBloc() : super(const CheckListState()) {
    on<CheckListEventFetch>(_onFetch);
    on<CheckForListUpdateEvent>(_onCheckForUpdate);
    on<CheckListEventSubmit>(_onSubmit);
    on<CheckListEventAgree>(_onAgree);
  }

  void _onFetch(CheckListEventFetch event, Emitter<CheckListState> emit) async {
    if (state.checkListItems.isNotEmpty) return;

    await for (var checkList in getIt<CheckListRepository>().getCheckList()) {
      emit(state.copyWith(checkListItems: _parseCheckListData(checkList)));
    }
  }

  void _onCheckForUpdate(
    CheckForListUpdateEvent event,
    Emitter<CheckListState> emit,
  ) async {
    if (event.editedItem.error == null) return;

    final result = await getIt<CheckListRepository>().getCheckListUpdate();
    emit(
      state.copyWith(
        checkListItems: _parseCheckListData(result),
      ),
    );
  }

  void _onSubmit(
    CheckListEventSubmit event,
    Emitter<CheckListState> emit,
  ) async {
    emit(state.copyWith(loading: true));
    try {
      await getIt<CheckListRepository>().submitVerification();
    } finally {
      emit(state.copyWith(loading: false));
    }
    emit(state.copyWith(isSubmitted: true));
  }

  void _onAgree(CheckListEventAgree event, Emitter<CheckListState> emit) {
    emit(state.copyWith(isAgree: !state.isAgree));
  }

  List<CheckListItemState> _parseCheckListData(Map<String, dynamic> checkList) {
    return [
      CheckListItemState(
          title: 'Personal Information'.tr(),
          error: checkList['personal_info'],
          route: PersonalInfoRoute(isInEditMode: true)),
      CheckListItemState(
          title: 'Emergency contact'.tr(),
          error: checkList['emergency_contacts'],
          route: EmergencyContactsRoute()),
      CheckListItemState(
          title: 'Roles'.tr(), error: checkList['roles'], route: RoleRoute()),
      if (checkList.containsKey('equipments'))
        CheckListItemState(
            title: 'Equipment'.tr(),
            error: checkList['equipments'],
            route: RoleKitFlowRoute(roleKitType: RoleKitType.equipment)),
      if (checkList.containsKey('uniforms'))
        CheckListItemState(
            title: 'Uniform'.tr(),
            error: checkList['uniforms'],
            route: RoleKitFlowRoute(roleKitType: RoleKitType.uniform)),
      CheckListItemState(
          title: 'Working Area'.tr(),
          error: checkList['working_areas'],
          route: WorkingAreaRoute()),
      CheckListItemState(
          title: 'Availability'.tr(),
          error: checkList['schedule'],
          route: ScheduleRoute()),
      CheckListItemState(
          title: 'Bank Account'.tr(),
          error: checkList['bank_account'],
          route: const BankAccountFlowRoute()),
      CheckListItemState(title: 'Wages form'.tr(), error: checkList['']),
      CheckListItemState(
          title: 'Certificates'.tr(),
          error: checkList['certificates'],
          route: const CertificatesRoute()),
      CheckListItemState(title: 'Background check'.tr(), error: checkList['']),
    ];
  }
}
