import { _decorator, Color, Component, instantiate, Label, Layout, Node, PageView, Prefab, Sprite, SpriteFrame, UITransform } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { tools } from '../../tools';
import { car_lib_list_item } from './car_lib_list_item';
import { car_lib_page_item } from './car_lib_page_item';
import { uiManager } from '../../manager/uiManager';
import { config } from '../../config';
import { GameManager } from '../../GameManager';
import { bag_type } from '../../data';
import { car_info } from '../car_info/car_info';
const { ccclass, property } = _decorator;

@ccclass('car_lib_bottom')
export class car_lib_bottom extends base_ui {
    @property(Node) btn_num_left:Node = null
    @property(Node) btn_num_right:Node = null
    @property(Node) lab_num:Node = null
    @property(Node) pageView:Node = null
    @property(Node) pageView_content:Node = null
    @property(Prefab) pageView_page_item:Prefab = null

    private num_current_count:number = 1
    private num_total_count:number = 1
    private current_select_list_item:car_lib_list_item = null

    private m_click_item_cb = null

    private game_status_parent:Node = null

    start() {
        this.onButtonListen(this.btn_num_left, ()=>{
            if(this.num_current_count==1) { 
                return 
            }
            this.num_current_count -=1
            this.updateNumStatus()
        })
        this.onButtonListen(this.btn_num_right, ()=>{
            if(this.num_current_count>=this.num_total_count) {
                return
            }
            this.num_current_count +=1
            this.updateNumStatus()
        })   
        this.pageView.on(PageView.EventType. PAGE_TURNING, (e:PageView)=>{
            let currentPageIndex = e.getCurrentPageIndex()
            // console.log('currentPageIndex=',currentPageIndex)
            this.num_current_count = currentPageIndex + 1
            this.updateNumStatus()
        }) 
    }

    public init(click_item_cb) {
        this.m_click_item_cb = click_item_cb
        this.num_total_count = tools.all_car_page_list.length
        this.updateNumStatus()

        let id = bag_type.car_suipian
        GameManager.requestBagList(id, (car_suipian_list)=>{
            // console.log('car_suipian_list=',car_suipian_list)
            for (let index = 0; index < this.num_total_count; index++) {
                let page = instantiate(this.pageView_page_item)
                let data_list = tools.all_car_page_list[index]
                page.getComponent(car_lib_page_item).init(this.pageView, index,data_list, car_suipian_list, this.onSelectedItem.bind(this), this.onClickListItem.bind(this))
                this.pageView.getComponent(PageView).addPage(page)
            }
        })
    }

    private getNumCurrentIndex():number {
        let index = this.num_current_count - 1
        if(index<0) {
            index = 0
        }
        return index
    }

    private updateNumStatus() {
        this.lab_num.getComponent(Label).string = this.num_current_count + '/' + this.num_total_count
        let index = this.getNumCurrentIndex()
        this.pageView.getComponent(PageView).scrollToPage(index)
    }

    private onSelectedItem(page_item:car_lib_page_item, list_item:car_lib_list_item) {
        this.current_select_list_item = list_item
        setTimeout(()=>{
            this.pageView.getComponent(PageView).scrollToPage(page_item.getIndex())
        },100)
    }

    private onClickListItem(page_item:car_lib_page_item,list_item:car_lib_list_item) {
        uiManager.Instance().showUi(config.UI.ui_car_info,this.game_status_parent!=null?this.game_status_parent:null, (node:Node)=>{
            let car_info_component = node.getComponent(car_info)
            car_info_component.initView(list_item.getData(),list_item.getIsJiesuo())
            car_info_component.initOperateNodeCallback((v:car_info)=>{
                // 装备
                if(this.current_select_list_item!=null) {
                    this.current_select_list_item.setUnselectedStatus()
                }
                list_item.setSelectedStatus()
                this.current_select_list_item = list_item
        
                if(this.m_click_item_cb!=null) {
                    this.m_click_item_cb(list_item)
                }
            },(v:car_info)=>{
                // 兑换
                let data = v.getData()
                if(data.temp_bag_list_item_data) {
                    GameManager.showExchangeCarView(data.id,data.temp_bag_list_item_data.icon,data.temp_bag_list_item_data.quantity, ()=>{
                        car_info_component.closeSelf()
                        list_item.setJiesuoSelectedStatus()
                    })
                }
            })
            car_info_component.initGetSuipianNodeCallback((v:car_info)=>{
                // 看视频
            }, (v:car_info)=>{
                // 分享
            })
        })

        // if(this.current_select_list_item!=null) {
        //     if(this.current_select_list_item.getData().id == list_item.getData().id) {
        //         return
        //     }
        // }
        
        // if(list_item.getIsJiesuo()==false) {
        //     uiManager.Instance().showUi(config.UI.ui_car_info, null, (node:Node)=>{
        //         node.getComponent(car_info).initView(list_item.getData(),list_item.getIsJiesuo())
        //     })
        //     return
        // }

        // if(this.current_select_list_item!=null) {
        //     this.current_select_list_item.setUnselectedStatus()
        // }
        // list_item.setSelectedStatus()
        // this.current_select_list_item = list_item

        // if(this.m_click_item_cb!=null) {
        //     this.m_click_item_cb(list_item)
        // }
    }

    public initGameStatusParent(parent:Node){
        this.game_status_parent = parent
    }
}

