import { ICharacterDailyTask, ICollectProgress, ICraftProgress, IKillMobsProgress, IMapVisitProgress, TaskStatus, TaskType } from '@rpg-engine/shared';
import { uiColors } from '../../../constants/uiColors';

export const formatTaskKey = (key: string): string => {
    let formatted = key.replace(/[-_]/g, ' ');
    formatted = formatted.replace(/([A-Z])/g, ' $1');
    formatted = formatted.trim();
    return formatted
        .split(' ')
        .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
        .join(' ');
};

export const getTaskIcon = (taskType: TaskType, difficulty?: string): string => {
    const MapIcon = 'map-2.png';
    const KillMobsIconRegular = 'white-skull.png';
    const KillMobsIconChallenge = 'red-skull.png';
    const CollectItemIcon = 'inventory-2.png';
    const CraftItemIcon = 'crafting.png';

    switch (taskType) {
        case TaskType.KillMobs:
            return difficulty === 'Challenge'
                ? KillMobsIconChallenge
                : KillMobsIconRegular;
        case TaskType.CollectItems:
            return CollectItemIcon;
        case TaskType.MapVisit:
            return MapIcon;
        case TaskType.CraftItems:
            return CraftItemIcon;
        default:
            return 'check.png';
    }
};

export const getStatusInfo = (
    task: ICharacterDailyTask
): { text: string; color: string } => {
    if (task.status === TaskStatus.Completed) {
        return { text: 'Completed', color: uiColors.lightGreen };
    }

    if (task.claimed) {
        return { text: 'Claimed', color: uiColors.yellow };
    }

    const isTaskNotStarted = (() => {
        switch (task.type) {
            case TaskType.KillMobs:
                return (
                    task.type === TaskType.KillMobs &&
                    Object.values((task.progress as IKillMobsProgress).kills || {}).every(
                        kill => kill === 0
                    )
                );

            case TaskType.CollectItems:
                return (
                    task.type === TaskType.CollectItems &&
                    Object.values(
                        (task.progress as ICollectProgress).collected || {}
                    ).every(quantity => quantity === 0)
                );

            case TaskType.CraftItems:
                return (
                    task.type === TaskType.CraftItems &&
                    Object.values((task.progress as ICraftProgress).crafted || {}).every(
                        quantity => quantity === 0
                    )
                );

            case TaskType.MapVisit:
                return (
                    task.type === TaskType.MapVisit &&
                    Object.values(
                        (task.progress as IMapVisitProgress).visitedMaps || {}
                    ).every(visited => !visited)
                );

            default:
                return false;
        }
    })();

    if (isTaskNotStarted) {
        return { text: 'Not Started', color: uiColors.lightGray };
    }

    return { text: 'In Progress', color: uiColors.blue };
};

export const formatDifficulty = (difficulty: string): string => {
    return difficulty.charAt(0).toUpperCase() + difficulty.slice(1).toLowerCase();
};