import 'package:krow/core/data/models/event/event_model.dart';
import 'package:krow/features/create_event/domain/bloc/create_event_bloc.dart';
import 'package:krow/features/create_event/presentation/create_role_section/bloc/create_role_bloc.dart';
import 'package:krow/features/create_event/presentation/create_shift_details_section/bloc/create_shift_details_bloc.dart';

class CreateEventInputValidator {
  static CreateEventState validateInputs(CreateEventState state) {
    var newState = _validateEvent(state);
    newState = _validateShifts(newState);
    return newState;
  }

  static CreateEventState _validateEvent(CreateEventState state) {
    EventValidationState? validationState;
    if (state.entity.name.isEmpty) {
      validationState = EventValidationState(nameError: 'Name cannot be empty');
    }

    if (state.entity.startDate == null) {
      validationState =
          EventValidationState(startDateError: 'Start Date cannot be empty');
    }

    if (state.recurringType == EventScheduleType.recurring &&
        state.entity.endDate == null) {
      validationState =
          EventValidationState(endDateError: 'End Date cannot be empty');
    }
    if (state.entity.hub == null) {
      validationState = EventValidationState(hubError: 'Hub cannot be empty');
    }
    // if (state.entity.contractType == EventContractType.contract &&
    //     (state.entity.contractNumber?.isEmpty ?? true)) {
    //   validationState = EventValidationState(
    //       contractNumberError: 'Contract Number cannot be empty');
    // }
    if ((state.entity.poNumber?.isEmpty ?? true)) {
      validationState =
          EventValidationState(poNumberError: 'PO Number cannot be empty');
    }

    if (state.shifts.isEmpty) {
      validationState =
          EventValidationState(shiftsError: 'Shifts cannot be empty');
    }

    if (state.validationState == null && validationState == null) {
      return state.copyWith(valid: true);
    } else {
      return state.copyWith(validationState: validationState);
    }
  }

  static CreateEventState _validateShifts(CreateEventState state) {
    for (var shift in state.shifts) {
      ShiftValidationState? validationState;
      if (!(shift.bloc.state.shift.fullAddress?.isValid() ?? false)) {
        validationState = ShiftValidationState(addressError: 'Invalid Address');
      }

      if (shift.bloc.state.shift.managers.isEmpty) {
        validationState =
            ShiftValidationState(contactsError: 'Managers cannot be empty');
      }

      if (validationState != null) {
        shift.bloc.add(ValidationFailedEvent(validationState));
        return state.copyWith(
            valid: false, validationState: state.validationState);
      }

      if (validatePosition(shift.bloc.state.roles)) {
        return state.copyWith(
            valid: false, validationState: state.validationState);
      }
    }
    return state;
  }

  static bool validatePosition(List<RoleViewModel> roles) {
    for (var position in roles) {
      PositionValidationState? validationState;

      if (position.bloc.state.entity.businessSkill == null) {
        validationState =
            PositionValidationState(skillError: 'Skill cannot be empty');
      }

      if (position.bloc.state.entity.department == null) {
        validationState = PositionValidationState(
            departmentError: 'Department cannot be empty');
      }

      if (position.bloc.state.entity.count == null) {
        validationState =
            PositionValidationState(countError: 'Count cannot be empty');
      }

      if (validationState != null) {
        position.bloc.add(ValidationPositionFailedEvent(validationState));
        return true;
      }
    }
    return false;
  }
}
