import request from './request'
import cache from './cacheKey.js'
import { getImageUrl } from '@/common/api/basic'
import { apiUpdatePush } from '@/common/api/user'
import { queryDictDataCache } from '../api/dictionary.js'
import cacheKey from './cacheKey.js'
import config from '@/common/js/config'
import jsrsasign from 'jsrsasign'

const toast = (title, duration = 1500, call, mask = false, icon = 'none') => {
    if (Boolean(title) === false) {
        return
    }
    uni.showToast({
        title,
        duration,
        mask,
        icon,
    })
    setTimeout(() => {
        call && call()
    }, duration)
}

/**
 * @description 格式化时间
 * @param time
 * @param cFormat
 * @returns {string|null}
 */
function parseTime(time, cFormat) {
    if (arguments.length === 0) {
        return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
            time = parseInt(time)
        }
        if (typeof time === 'number' && time.toString().length === 10) {
            time = time * 1000
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
    }
    return format.replace(/{([ymdhisa])+}/g, (result, key) => {
        let value = formatObj[key]
        if (key === 'a') {
            return ['日', '一', '二', '三', '四', '五', '六'][value]
        }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
}

/**
 * @description 格式化时间
 * @param time
 * @param option
 * @returns {string}
 */
function formatTime(time, option) {
    if (('' + time).length === 10) {
        time = parseInt(time) * 1000
    } else {
        time = +time
    }
    if (option) {
        return parseTime(time, option)
    } else {
        const d = new Date(time)
        const now = Date.now()
        const diff = (now - d) / 1000
        if (diff < 30) {
            return '刚刚'
        } else if (diff < 3600) {
            // less 1 hour
            return Math.ceil(diff / 60) + '分钟前'
        } else if (diff < 3600 * 24) {
            return Math.ceil(diff / 3600) + '小时前'
        } else if (diff < 3600 * 24 * 2) {
            return '1天前'
        }
        return (
            d.getMonth() + 1 + '月' + d.getDate() + '日'
            // +
            // d.getHours() +
            // '时'
            //  +
            // d.getMinutes() +
            // '分'
        )
    }
}
// 获取当天日期（年月日）
function getToday() {
    const today = new Date()
    const year = today.getFullYear()
    const month = String(today.getMonth() + 1).padStart(2, '0') // 月份从0开始，需+1
    const day = String(today.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
}
// 获取当天日期 往前推30天
function getBeforeDays(days = 30) {
    const today = new Date()
    // 计算往前推N天的时间戳（1天=86400000毫秒）
    const beforeDate = new Date(today.getTime() - days * 24 * 60 * 60 * 1000)
    return formatDate(beforeDate)
}
function formatDate(date) {
    const year = date.getFullYear()
    // 月份从0开始，补零到2位
    const month = String(date.getMonth() + 1).padStart(2, '0')
    // 日期补零到2位
    const day = String(date.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
}

// 获取3个月前的日期
function getThreeMonthsAgo() {
    const threeMonthsAgo = new Date()
    threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3) // 月份减3
    const year = threeMonthsAgo.getFullYear()
    const month = String(threeMonthsAgo.getMonth() + 1).padStart(2, '0')
    const day = String(threeMonthsAgo.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
}
//获取月最后一天
function getMonthFirstAndLastDay(monthStr) {
    // 1. 校验输入格式（正则匹配YYYY-MM）
    const reg = /^\d{4}-\d{2}$/
    if (!reg.test(monthStr)) {
        throw new Error('输入格式错误，请传入"YYYY-MM"格式的字符串，例如：2026-03')
    }

    // 2. 拆分年、月
    const [year, month] = monthStr.split('-').map(Number)

    // 3. 生成当月第一天（直接拼接01）
    const firstDay = `${year}-${String(month).padStart(2, '0')}-01`

    // 4. 生成当月最后一天（核心：利用Date的特性，下个月0号 = 当月最后一天）
    // 注意：月份是0开始的（0=1月，11=12月），所以month+1是下一个月
    const lastDayDate = new Date(year, month, 0) // 下个月0号 = 当月最后一天
    const lastDay = `${year}-${String(month).padStart(2, '0')}-${String(lastDayDate.getDate()).padStart(2, '0')}`

    return {
        firstDay,
        lastDay,
    }
}

const h5Helper = {
    isAndroid: function () {
        return window.navigator.appVersion.toLowerCase().indexOf('android') != -1
    },
    isIOS: function () {
        return window.navigator.appVersion.toLowerCase().indexOf('iphone') != -1
    },
    isWeiXinWeb: function () {
        var ua = window.navigator.userAgent.toLowerCase()
        return ua.match(/MicroMessenger/i) == 'micromessenger'
    },
}

// 验证手机号格式
const validatePhoneNumber = (phone) => {
    if (!phone) return false
    var testReg = /^1[23456789]\d{9}$/
    return testReg.test(phone)
}


// 加载用户配置
var globalConfigIsLoading = false,
    global_config = null,
    globalConfigCallbacks = []
/**
 * 加载用户配置
 * @param call  加载成功后的回调
 * @param need_fresh 是否及时刷新用户配置
 */
const loadConfig = (call, need_fresh = false) => {
    if (call && global_config && !need_fresh) {
        call(global_config)
        return
    }
    if (globalConfigIsLoading) {
        globalConfigCallbacks.push(call)
        return
    }
    globalConfigIsLoading = true
    request({
        url: '/org/userResource/userMsg',
    })
        .then((rs) => {
            globalConfigIsLoading = false
            global_config = rs.data
            uni.setStorage({
                key: 'userInfo',
                data: global_config,
            })
            if (call) {
                call(global_config)
            }
            for (var i = 0; i < globalConfigCallbacks.length; i++) {
                globalConfigCallbacks[i](global_config)
            }
            globalConfigCallbacks = []
        })
        .catch((err) => {
            globalConfigIsLoading = false
            console.warn(err)
            // uni.reLaunch({ url: '/pages/user/login' })
        })
}

const prePage = () => {
    let pages = getCurrentPages()
    let prePage = pages[pages.length - 2]
    return prePage
}

const loginSuccess = (data, jump = true) => {
    return new Promise((resolve, reject) => {
        console.log(data)
        uni.setStorageSync('access_token', data.token_type + ' ' + data.access_token)
        uni.setStorageSync('refresh_token', data.refresh_token)
        let userInfo = decodeToken(data.access_token)
        console.log(userInfo)
        // let strings = data.access_token.split('.') //截取token，获取载体
        // console.log(escape, atob)
        // var userInfo = JSON.parse(decodeURIComponent(escape(atob(strings[1].replace(/-/g, '+').replace(/_/g, '/')))))
        userInfo.authorities = userInfo.authorities[0]
        if (userInfo.headSculpture) {
            userInfo.avatar = config.static + userInfo.headSculpture
        }
        console.log(userInfo)
        uni.setStorageSync(cache.userInfo, userInfo)
        apiUpdatePush()
        resolve(userInfo)
        if (jump) {
            queryDictDataCache().then((res) => {
                uni.setStorageSync(cacheKey.dictData, res.data)
            })
            console.log('reLaunch')
            uni.reLaunch({
                url: '/pages/index/index',
                fail: (err) => {
                    console.log(err)
                },
            })
        }
    })
}
/**
 * 只针对列表页的刷新
 * @param {*} number
 * @param {*} time
 */
const refreshPrePage = (number = 1, time = 1500) => {
    let pages = getCurrentPages()
    let prePage = pages[pages.length - number - 1]
    if (prePage && time) {
        prePage.$vm.store?.reload()
        setTimeout(() => {
            uni.navigateBack({
                delta: number,
            })
        }, time)
    }
}

const decodeToken = (token) => {
    let obj = null
    if (token !== '') {
        const payload = jsrsasign.KJUR.jws.JWS.parse(token)
        if (payload.hasOwnProperty('payloadObj')) {
            obj = payload.payloadObj
        }
    }
    return obj
}

const getDictData = (key) => {
    return new Promise((resolve, reject) => {
        let dictData = uni.getStorageSync(cacheKey.dictData)
        if (dictData) {
            resolve(dictData.filter((item) => item.code === key)[0]?.children || [])
        } else {
            // 查询字典
            queryDictDataCache()
                .then((res) => {
                    uni.setStorageSync(cacheKey.dictData, res.data)
                    resolve(res.data.filter((item) => item.code === key)[0]?.children || [])
                })
                .catch((err) => {
                    reject(err)
                })
        }
    })
}

export default {
    validatePhoneNumber,
    toast,
    formatTime,
    parseTime,
    h5Helper,
    loadConfig,
    prePage,
    loginSuccess,
    refreshPrePage,
    getDictData,
    getToday,
    getBeforeDays,
    getThreeMonthsAgo,
    getMonthFirstAndLastDay,
}
