import { _decorator, Component, instantiate, Layout, Node, Prefab, UITransform } from 'cc';
import { bag_list_item_data, bag_type } from '../../data';
import { bag_list_page_item } from './bag_list_page_item';
import { GameManager } from '../../GameManager';
import { uiManager } from '../../manager/uiManager';
import { config } from '../../config';
import { bag_avatar_preview } from './bag_avatar_preview';
const { ccclass, property } = _decorator;

@ccclass('bag_list_page')
export class bag_list_page extends Component {
    @property(Node) bg:Node = null
    @property(Prefab) page_item:Prefab = null
    private cur_xiangkuang_item:bag_list_page_item = null
    start() {
        let fixed_count = 4
        let item = instantiate(this.page_item)
        let spacing_x = (this.bg.getComponent(UITransform).width - fixed_count*item.getComponent(UITransform).width) / (fixed_count + 1)
        let bg_layout = this.bg.getComponent(Layout)
        bg_layout.spacingX = spacing_x
        bg_layout.paddingLeft = spacing_x
    }

    initView(data_list:bag_list_item_data[]) {
        for (let index = 0; index < data_list.length; index++) {
            const element = data_list[index];
            let item = instantiate(this.page_item)
            item.parent = this.bg
            let item_com = item.getComponent(bag_list_page_item)
            item_com.initView(element,this.onClickItem.bind(this))
            if(element.stype==bag_type.xiangkuang) {
                if(element.usage_status==1) {
                    item_com.setSelectedStatus()
                    this.cur_xiangkuang_item = item_com
                } else {
                    item_com.setUnselectedStatus()
                }
            }
        }
    }

    private onClickItem(item:bag_list_page_item) {
        let data = item.getData()
        switch (data.stype) {
            case bag_type.car_suipian:
                GameManager.showExchangeCarView(data.car_id,data.icon,data.quantity,()=>{
                    data.quantity -= data.quantity
                    item.setData(data)
                })
                break;
            case bag_type.xiangkuang:
                uiManager.Instance().showUi(config.UI.ui_bag_avatar_preview, null, (node:Node)=>{
                    node.getComponent(bag_avatar_preview).initView(data, ()=>{
                        if(this.cur_xiangkuang_item!=null) {
                            let cur_data = this.cur_xiangkuang_item.getData()
                            cur_data.usage_status = 0
                            this.cur_xiangkuang_item.setData(cur_data)
                            this.cur_xiangkuang_item.setUnselectedStatus()
                        }
                        data.usage_status = 1
                        item.setData(data)
                        item.setSelectedStatus()
                        this.cur_xiangkuang_item = item
                    })
                })   
                break;
            case bag_type.daoju:
                break;
            default:
                break;
        }
    }
}

