import Taro from "@tarojs/taro";
import indexStore from "@src/store/index";
import { setApp } from "@src/config";



/**
 * 获取短篇首页banners列表
 * */
export function getShortBanners(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getBanners',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexBanners: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 * 获取短篇首页热门分类列表
 * */
export function getShortHotCategory(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getHotCategory',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexHotCategory: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 *  获取重磅新书 
 * */
export function getShortNewBooks(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getNewBooks',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexNewBooks: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 *  获取热门推荐
 * */
export function getShortHotRec(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getHotRec',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexHotRec: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 *  获取口碑佳作
 * */
export function getShortBestReviews(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getBestReviews',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexBestReviews: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 *  获取经典热书
 * */
export function getShortClassicBooks(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getClassicBooks',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexClassicBooks: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 *  获取必看
 * */
export function getShortBestBooks(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getBestBooks',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexBestBooks: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 *  猜你喜欢
 * */
export function getShortGuessLike(data) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getGuessLike',
                method: 'GET',
                data,
                success: (res) => {
                    indexStore.setData({ indexGuessLike: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
/**
 *获取短篇首页热门书籍列表
 * */
export function getShortHotBooks(workDirection) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appComponent/getHotBooks',
                method: 'GET',
                data: { workDirection },
                success: (res) => {
                    indexStore.setData({ indexHotBooks: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 * 获取充值模板配置
 * */
export function getRechargeTemplate() {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/rechargeTemplate/getInfo',
                method: 'GET',
                success: (res) => {
                    indexStore.setData({ rechargeTemplate: res.data.data })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
/**
 * 获取枚举
 * */
export function enumDictList() {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/enumDict/list',
                method: 'GET',
                success: (res) => {
                    let enumList = {}
                    Object.values(res.data?.data?.enums).map((item: any) => {
                        enumList[item.name] = item
                    })
                    setApp({ enumDictList: enumList })
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}

/**
 * 分享信息获取
 * @param path 路径
 * @param bookId 书籍ID
*/
export function getShareInfo(data) {
    return new Promise(async (resolve, reject) => {
        try {
            // 初始化
            Taro.request({
                url: '/app/appPage/getShareInfo',
                method: 'GET',
                data,
                success: (res) => {
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}