import dayjs from 'dayjs'

/**
 * 天气现象编码映射
 */
export const WEATHER_CODE_MAPPING = {
    '1': {
        code: '1',
        text: '晴',
    },
    '2': {
        code: '2',
        text: '多云',
    },
    '3': {
        code: '3',
        text: '阴',
    },
    '4': {
        code: '4',
        text: '雷阵雨',
    },
    '5': {
        code: '5',
        text: '冰雹',
    },
    '6': {
        code: '6',
        text: '雨夹雪',
    },
    '7': {
        code: '7',
        text: '阵雨',
    },
    '8': {
        code: '8',
        text: '小雨',
    },
    '9': {
        code: '9',
        text: '中雨',
    },
    '10': {
        code: '10',
        text: '大雨',
    },
    '11': {
        code: '11',
        text: '暴雨',
    },
    '12': {
        code: '12',
        text: '阵雪',
    },
    '13': {
        code: '13',
        text: '小雪',
    },
    '14': {
        code: '14',
        text: '中雪',
    },
    '15': {
        code: '15',
        text: '大雪',
    },
    '16': {
        code: '16',
        text: '暴雪',
    },
    '17': {
        code: '17',
        text: '轻雾',
    },
    '18': {
        code: '18',
        text: '雾',
    },
    '19': {
        code: '19',
        text: '大雾',
    },
    '20': {
        code: '20',
        text: '浓雾',
    },
    '21': {
        code: '21',
        text: '冻雨',
    },
    '22': {
        code: '22',
        text: '沙尘暴',
    },
    '23': {
        code: '23',
        text: '浮尘',
    },
    '24': {
        code: '24',
        text: '扬沙',
    },
    '25': {
        code: '25',
        text: '霾',
    },
    '26': {
        code: '26',
        text: '中度霾',
    },
    '27': {
        code: '27',
        text: '重度霾',
    },
    '28': {
        code: '28',
        text: '严重霾',
    },
    '29': {
        code: '29',
        text: '雷电',
    },
    '30': {
        code: '30',
        text: '龙卷风',
    },
}

export function getWeatherIcon(code: string) {
    // const nightIcon = ['1', '2', '4', '7', '12']
    // const isNightFlag = dayjs().hour() >= 18 && nightIcon.includes(code)
    // return `static/images/weather/${isNightFlag ? 'night' : 'day'}${code}.png`
    return `static/images/weather/${code}.svg`
}

export function getDayLabelValue(index: number) {
    const today = dayjs()
    const targetDate = today.add(index, 'day')
    const dayOfWeek = targetDate.day()

    let dayLabel = ''
    if (index === 0) {
        dayLabel = '今天'
    } else if (index === 1) {
        dayLabel = '明天'
    } else {
        dayLabel = dayjs().day(dayOfWeek).format('dddd')
    }

    return {
        label: dayLabel,
        value: targetDate.format('MM/DD'),
    }
}

export function getWindSpeedLevel(speed) {
    if (speed < 1) {
        return '无风'
    } else if (speed < 6) {
        return '软风'
    } else if (speed < 12) {
        return '轻风'
    } else if (speed < 20) {
        return '微风'
    } else if (speed < 29) {
        return '和风'
    } else if (speed < 39) {
        return '清风'
    } else if (speed < 50) {
        return '强风'
    } else if (speed < 62) {
        return '疾风'
    } else if (speed < 75) {
        return '大风'
    } else if (speed < 89) {
        return '烈风'
    } else if (speed < 103) {
        return '狂风'
    } else {
        return '飓风'
    }
}

export function getWindDirectionText(direction) {
    const directions = ['北', '东北', '东', '东南', '南', '西南', '西', '西北']
    const index = Math.round(direction / 45) % 8
    return directions[index]
}
