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

interface BookParams {
    workDirection: string,//频道
    categoryId: number,//分类ID
    pageNum: number,//分页
    pageSize: number//页数
}
/**
 * 获取短篇小说
 * */
export function getShortBookInfoAppListOfPage(data: BookParams) {
    return new Promise(async (resolve, reject) => {
        try {
            Taro.request({
                url: '/app/shortBookInfo/appListOfPage',
                method: 'GET',
                data,
                success: (res) => {
                    bookStore.setData({ bookList: res.data.data })
                    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);
            }
        })
    })
}