use super::super::import_outcome::record_has_world_state_condition_rows;
use crate::documents::{RuntimeImportBlocker, RuntimeImportContext};
use crate::event::conditions::RuntimeCondition;
use crate::event::targets::RuntimeChairmanTarget;
use crate::inspect::smp::events::SmpLoadedPackedEventRecordSummary;

pub(crate) fn packed_record_condition_scope_import_blocker(
    record: &SmpLoadedPackedEventRecordSummary,
    runtime_context: &RuntimeImportContext,
) -> Option<RuntimeImportBlocker> {
    if record.standalone_condition_rows.is_empty() {
        return None;
    }

    let ordinary_condition_row_count = record
        .standalone_condition_rows
        .iter()
        .filter(|row| row.raw_condition_id >= 0)
        .count();
    if ordinary_condition_row_count != 0 {
        if ordinary_condition_row_count != record.decoded_conditions.len() {
            return Some(if record_has_world_state_condition_rows(record) {
                RuntimeImportBlocker::UnmappedWorldCondition
            } else {
                RuntimeImportBlocker::UnmappedOrdinaryCondition
            });
        }
        if (!runtime_context.has_territory_context)
            && (record
                .standalone_condition_rows
                .iter()
                .any(|row| row.requires_candidate_name_binding)
                || record.decoded_conditions.iter().any(|condition| {
                    matches!(
                        condition,
                        RuntimeCondition::TerritoryNumericThreshold { .. }
                            | RuntimeCondition::CompanyTerritoryNumericThreshold { .. }
                    )
                }))
        {
            return Some(RuntimeImportBlocker::MissingTerritoryContext);
        }
    }

    let negative_sentinel_row_count = record
        .standalone_condition_rows
        .iter()
        .filter(|row| row.raw_condition_id == -1)
        .count();
    if negative_sentinel_row_count == 0 {
        return if ordinary_condition_row_count == 0 {
            Some(RuntimeImportBlocker::MissingConditionContext)
        } else {
            None
        };
    }
    if ordinary_condition_row_count == 0
        && negative_sentinel_row_count != record.standalone_condition_rows.len()
    {
        return Some(RuntimeImportBlocker::MissingConditionContext);
    }

    if record.negative_sentinel_scope.is_none() {
        return Some(RuntimeImportBlocker::MissingConditionContext);
    }

    None
}

pub(crate) fn chairman_target_import_blocker(
    target: &RuntimeChairmanTarget,
    runtime_context: &RuntimeImportContext,
) -> Option<RuntimeImportBlocker> {
    match target {
        RuntimeChairmanTarget::AllActive => {
            if runtime_context.known_chairman_profile_ids.is_empty() {
                Some(RuntimeImportBlocker::MissingChairmanContext)
            } else {
                None
            }
        }
        RuntimeChairmanTarget::HumanChairmen | RuntimeChairmanTarget::AiChairmen => {
            if runtime_context.known_chairman_profile_ids.is_empty() {
                Some(RuntimeImportBlocker::MissingChairmanContext)
            } else if !runtime_context.has_complete_company_controller_context {
                Some(RuntimeImportBlocker::MissingCompanyRoleContext)
            } else {
                None
            }
        }
        RuntimeChairmanTarget::SelectedChairman => {
            if runtime_context.selected_chairman_profile_id.is_some() {
                None
            } else {
                Some(RuntimeImportBlocker::MissingChairmanContext)
            }
        }
        RuntimeChairmanTarget::ConditionTrueChairman => {
            if runtime_context.known_chairman_profile_ids.is_empty() {
                Some(RuntimeImportBlocker::MissingChairmanContext)
            } else {
                None
            }
        }
        RuntimeChairmanTarget::Ids { ids } => {
            if runtime_context.known_chairman_profile_ids.is_empty() {
                Some(RuntimeImportBlocker::MissingChairmanContext)
            } else if ids
                .iter()
                .all(|id| runtime_context.known_chairman_profile_ids.contains(id))
            {
                None
            } else {
                Some(RuntimeImportBlocker::MissingChairmanContext)
            }
        }
    }
}
