import { _decorator, Component, instantiate, Label, Node, Prefab, utils } from 'cc';
import { http } from '../http';
import { config } from '../config';
import { level_list_item_data, long_story_list_item_data } from '../../data/data';
import { level_list_item } from './level_list_item';
import { tools } from '../tools';
const { ccclass, property } = _decorator;

@ccclass('level_list')
export class level_list extends Component {
    @property(Node) btn_back:Node = null;
    @property(Node) lab_title:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item:Prefab = null;
    @property(Node) page_number:Node = null;
    @property(Node) btn_previous_page:Node = null;
    @property(Node) lab_current_page:Node = null;
    @property(Node) btn_next_page:Node = null;
 
    private m_long_story_list_data: long_story_list_item_data = null
    
    private m_back_call_back = null;
    private m_click_callback = null;

    private m_cur_page:number = 1;
    private m_every_page_count:number = 18;
    private m_total_page_count:number = 0;

    protected start(): void {
        this.btn_back.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_back_call_back) {
                this.m_back_call_back()
            }
        })

        this.page_number.active = false
        let self = this
        this.btn_previous_page.on(Node.EventType.TOUCH_END, ()=> {
            if(self.m_cur_page == 1) {
                tools.showToast("已是第一页")
                return
            }
            self.m_cur_page-=1
            self.requestData()
        },this)
        this.btn_next_page.on(Node.EventType.TOUCH_END, ()=> {
            if(self.m_cur_page >= self.m_total_page_count) {
                tools.showToast("已是最后一页")
                return
            }
            self.m_cur_page+=1
            self.requestData()
        },this)
    }

    public init(long_story_list_data: long_story_list_item_data, back_call_back, call_back){
        this.m_long_story_list_data = long_story_list_data
        if(this.m_long_story_list_data) {
            this.lab_title.getComponent(Label).string = `${this.m_long_story_list_data.name}-关卡列表`
        } else {
            this.lab_title.getComponent(Label).string = '关卡列表'
        }
        this.m_back_call_back = back_call_back;
        this.m_click_callback = call_back;
        this.requestData()
    }

    initView(list:level_list_item_data[]){
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let node = instantiate(this.item)
            node.parent = this.content;
            node.getComponent(level_list_item).initView(element,this.m_click_callback)
        }
    }

    requestData() {
        this.content.removeAllChildren()

        let stype = config.LevelType.LEVEL  //0:关卡,1:长篇
        let book_id = 0
        if(this.m_long_story_list_data) {
            stype = config.LevelType.LONG_STORY
            book_id = this.m_long_story_list_data.id
        }
        let opt = {"stype":stype,"book_id":book_id,"page":this.m_cur_page,"limit":this.m_every_page_count}
        // console.log('opt=',opt)
        http.post("/tool/mysnote/levels",opt,(err,data)=>{
            if(!err){
                let _data = JSON.parse(data);
                // console.log('levels data=',_data)
                let __data =  _data.content
                if(this.m_cur_page == 1) {
                    this.page_number.active = true
                    this.m_total_page_count = Math.ceil(__data.tool_number/this.m_every_page_count)
                }
                this.setupLabNumber()
                this.initView(__data.list)
                config.last_id = parseInt(_data.content.zujian_id)
                console.log("level_list",config.last_id)
            }
        })
    }

    setupLabNumber() {
        let string = this.m_cur_page + ' / ' + this.m_total_page_count
        this.lab_current_page.getComponent(Label).string = string
    }
}


