import { _decorator, Component, instantiate, Label, Layout, Node, 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 { userDataManager } from '../../manager/userDataManager';
import { car_item_data } from '../../data';
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) btn_score_sort:Node = null
    @property(SpriteFrame) sf_score_up:SpriteFrame = null
    @property(SpriteFrame) sf_score_down:SpriteFrame = null
    @property(Node) btn_jiesuo:Node = null
    @property(SpriteFrame) sf_jiesuo_select:SpriteFrame = null
    @property(SpriteFrame) sf_jiesuo_unselect:SpriteFrame = null
    @property(Node) list:Node = null
    @property(Node) list_content:Node = null
    @property(Prefab) list_item:Prefab = null

    private data_list:car_item_data[] = []
    private is_sort_up_status:boolean = true
    private is_jiesuo_status:boolean = false
    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

    start() {
        this.onButtonListen(this.btn_num_left, ()=>{
            if(this.num_current_count==1) { 
                return 
            }
            this.num_current_count -=1
            this.updateNumStatus(true)
        })
        this.onButtonListen(this.btn_num_right, ()=>{
            if(this.num_current_count>=this.num_total_count) {
                return
            }
            this.num_current_count +=1
            this.updateNumStatus(true)
        })
        this.onButtonListen(this.btn_score_sort, ()=>{
            this.is_sort_up_status = !this.is_sort_up_status
            this.updateSortStatus(true)
        })
        this.onButtonListen(this.btn_jiesuo, ()=>{
            this.is_jiesuo_status = !this.is_jiesuo_status
            this.updateJiesuoStatus(true)
        })        
    }

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

        // this.data_list = JSON.parse(JSON.stringify(tools.all_car_list))
        this.data_list = tools.all_car_page_list[this.getNumCurrentIndex()]
        this.updateSortStatus()
        this.updateJiesuoStatus()
        this.updateNumStatus()
        this.reloadListContentData()

        let list_content_size = this.list_content.getComponent(UITransform).contentSize
        let item_contenteSize = instantiate(this.list_item).getComponent(UITransform).contentSize
        let horizontal_padding = (list_content_size.width - item_contenteSize.width * 2) / 3
        if(horizontal_padding > 0) {
            this.list_content.getComponent(Layout).paddingLeft = horizontal_padding
            this.list_content.getComponent(Layout).paddingRight = horizontal_padding
            this.list_content.getComponent(Layout).spacingX = horizontal_padding
        }
    }

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

    updateSortStatus(is_load_data:boolean=false) {
        if(this.is_sort_up_status) {
            this.btn_score_sort.getComponent(Sprite).spriteFrame = this.sf_score_down
        } else {
            this.btn_score_sort.getComponent(Sprite).spriteFrame = this.sf_score_up
        }
        if(is_load_data) {
            this.data_list = this.data_list.reverse()
            this.reloadListContentData()
        }
    }

    updateJiesuoStatus(is_load_data:boolean=false) {
        if(this.is_jiesuo_status) {
            this.btn_jiesuo.getComponent(Sprite).spriteFrame = this.sf_jiesuo_select
        } else {
            this.btn_jiesuo.getComponent(Sprite).spriteFrame = this.sf_jiesuo_unselect
        }
        if(is_load_data) {
            this.reloadListContentData()
        }
    }

    updateNumStatus(is_reload_data:boolean=false) {
        this.lab_num.getComponent(Label).string = this.num_current_count + '/' + this.num_total_count
        if(is_reload_data) {
            this.current_select_list_item = null
            this.data_list = tools.all_car_page_list[this.getNumCurrentIndex()]
            this.reloadListContentData()
        }
    }

    reloadListContentData() {
        this.list_content.removeAllChildren()
        this.current_select_list_item = null
        for (let index = 0; index < this.data_list.length; index++) {
            const element = this.data_list[index];
            let is_jiesuo = userDataManager.user_car_list.car_list.some(obj => obj === element.id)
            let item = null
            if(this.is_jiesuo_status) {
                if(is_jiesuo) {
                    item = instantiate(this.list_item)
                }
            } else {
                item = instantiate(this.list_item)
            }
            if(item!=null) {
                item.parent = this.list_content
                let item_component = item.getComponent(car_lib_list_item)
                item_component.initView(element, index, this.onClickListItem.bind(this))
                if(is_jiesuo) {
                    item_component.setJiesuoSelectedStatus()
                } else {
                    item_component.setJiesuoUnselectedStatus()
                }
                if(userDataManager.user_car_list.default_car_id == element.id) {
                    this.current_select_list_item = item_component
                    item_component.setSelectedStatus()
                } else {
                    item_component.setUnselectedStatus()
                }
            }
        }
    }

    onClickListItem(item:car_lib_list_item) {
        if(this.current_select_list_item!=null) {
            if(this.current_select_list_item.getData().id == item.getData().id) {
                return
            }
        }
        
        if(item.getIsJiesuo()==false) {
            console.log('解锁 解锁 解锁')
            return
        }

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

        if(this.m_click_item_cb!=null) {
            this.m_click_item_cb(item)
        }
    }

}

