
import Taro from "@tarojs/taro";
import bookStore from "@src/store/book";
import classifyStore from "@src/store/classIfy";
import { setReadLog } from "@src/utils/loginSto";

interface BookParams {
    workDirection: 0 | 1,//频道
    categoryId: number,//分类ID
    pageNum: number,//分页
    pageSize: number//页数
    type: 'index' | 'class',//区分存放至哪个store
}
/**
 * 获取短篇小说
 * */
export function getShortBookInfoAppListOfPage(data: BookParams) {
    let { type, ...newData } = data
    return new Promise(async (resolve, reject) => {
        try {
            Taro.request({
                url: '/app/shortBookInfo/appListOfPage',
                method: 'GET',
                data: data,
                success: (res) => {
                    switch (type) {
                        case 'index':
                            bookStore.setData({ bookList: res.data.data })
                            break;
                        case 'class':
                            classifyStore.setData({ bookList: res.data.data })
                            break;
                    }
                    resolve(res)
                },
                fail: (err) => {
                    reject(err);
                }
            })
        } catch (error) {
            reject(error);
        }
    })
}
interface ParagraphListParmas {
    pageSize: number,
    pageNum: number,
}
/**
 * 短篇小说内容列表获取
 * */
export function getParagraphList(data: ParagraphListParmas) {
    return new Promise(async (resolve, reject) => {
        Taro.request({
            url: '/app/shortBookInfo/paragraphList',
            method: 'GET',
            data: { ...data, backContent: true, bookId: bookStore.openBookData?.bookId },
            success: (res) => {
                bookStore.setData({ openBookData: { ...bookStore.openBookData, contentData: res.data.data } })
                resolve(res)
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}
/**
 * 短篇小说详情
 * */
export function getShortBookInfo(bookId: any) {
    return new Promise(async (resolve, reject) => {
        Taro.request({
            url: `/app/shortBookInfo/getInfo/${bookId}`,
            method: 'GET',
            success: (res) => {
                let readLogId = res.data.data?.readLogId
                if (readLogId) {
                    setReadLog({ [bookId]: readLogId })
                }
                bookStore.setData({ openBookData: res.data.data })
                resolve(res)
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

/**
 * 开始阅读小说内容上报
 * @param bookId 小说ID
 * @param readCondition 场景
 * */
export function startReadShort(data: { bookId: any, readCondition: any }) {
    return new Promise(async (resolve, reject) => {
        Taro.request({
            url: `/app/shortBookInfo/startRead`,
            method: 'GET',
            data,
            success: (res) => {
                bookStore.setData({ readId: res.data.data })
                resolve(res)
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

/**
 * 阅读中心跳上报
 * */
export function readingShort(params: { readId: number, readLogId: string, updateReadLog?: boolean }) {
    let { readId, readLogId, updateReadLog } = params
    return new Promise(async (resolve, reject) => {
        Taro.request({
            url: `/app/shortBookInfo/reading/${readId}?readLogId=${readLogId}&updateReadLog=${updateReadLog || false}`,
            method: 'PUT',
            success: (res) => {
                resolve(res)
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}