import { observable } from 'mobx'

interface ContentData {
    current: number,
    total: number,
    size: number,
    records: any[]
}
export interface OpenBookData {
    bookId: number,//书ID
    authorName: string,//作者
    bookName: string,//书名称
    bookDesc: string,//描述
    picUrl: string,//封面
    bookStatus: number,//完本|连载状态
    wordCount: number,//字数
    labelInfoList: { name: string }[],//标签
    contentData: ContentData | null //内容数据
    vipFree: boolean//vip免费
}

interface ClassifyData {
    id: number,
    name: string,//分类名称
    workDirection: number,//频道
}


export interface BookStore {
    /**当前打开的的小说数据*/
    openBookData: OpenBookData | null,
    /**心跳上报ID*/
    readId: number,//
    bookList: any,
    /**1女频 0男频 默认男频*/
    workDirection: 0 | 1,
    oldWorkDirection?: 0 | 1,
    /**分类列表*/
    classifyData: ClassifyData[],
    /**阅读背景*/
    bookConfig: {
        bg: string,//背景
        lh: string,//行高
        off_on: boolean,//开关灯 
        size: string,//行高
    }
    /**设置对应数据*/
    setData: (data: { [key: string]: any }) => void
    /**设置当前打开的小说*/
    setOpenBookData: (data: OpenBookData) => void,
    /**设置当前选中男女频道*/
    setWorkDirection: (workDirection: 0 | 1) => void,
    /**设置分类列表*/
    setClassifyData: (classifyData: ClassifyData[]) => void
}
/**
 * 书籍信息
 */
const Book: BookStore = observable({
    openBookData: null,
    workDirection: 0,
    oldWorkDirection: undefined,
    classifyData: [],
    bookList: [],
    readId: 0,
    bookConfig: { size: 'pre_21', off_on: false, bg: 'bg_b', lh: 'lh_72' },
    setData(data: { [key: string]: any }) {
        if (data) {
            Object.keys(data).forEach(key => {
                //数据不同才存放
                if (JSON.stringify(this[key]) !== JSON.stringify(data[key])) {
                    this[key] = data[key]
                }
            })
        }
    },
    setOpenBookData(openBookData: OpenBookData) {
        if (openBookData) {
            if(JSON.stringify(this.openBookData) !== JSON.stringify(openBookData)){
                this.openBookData = openBookData
            }
        }
    },
    setWorkDirection(workDirection: 0 | 1) {
        this.workDirection = workDirection
    },
    setClassifyData(classifyData: ClassifyData[]) {
        if (classifyData) {
            if(JSON.stringify(this.classifyData) !== JSON.stringify(classifyData)){
                this.classifyData = classifyData
            }
        }
    }
})

export default Book