import { _decorator, Component, Label, Node, Sprite, UIOpacity } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { car_item_data, car_type } from '../../data';
import { imageCacheManager } from '../../manager/imageCacheManager';
import { userDataManager } from '../../manager/userDataManager';
import { car_info_shuxing } from './car_info_shuxing';
import { dataManager } from '../../manager/dataManager';
const { ccclass, property } = _decorator;

@ccclass('car_info')
export class car_info extends base_ui {
    @property(Node) public_bg:Node = null
    @property(Node) btn_close:Node = null
    @property(Node) lab_xiangcha_score:Node = null
    @property(Node) img_car:Node = null
    @property(Node) lab_car_name:Node = null
    @property(Node) lab_score:Node = null

    @property(Node) img_suo:Node = null
    @property(Node) shuxing_info:Node = null

    @property(Node) operate_btn_node:Node = null
    @property(Node) btn_zhuangbei:Node = null
    @property(Node) btn_duihuang:Node = null

    @property(Node) get_suipian_node:Node = null
    @property(Node) btn_look_video:Node = null
    @property(Node) btn_share:Node = null

    private m_data:car_item_data = null
    private m_zhuangbei_cb = null
    private m_duihuan_cb = null
    private m_look_video_cb = null
    private m_share_cb = null

    start() {
        this.onButtonListen(this.public_bg, ()=>{
            this.close()
        })
        this.onButtonListen(this.btn_close, ()=>{
            this.close()
        })
        this.onButtonListen(this.btn_zhuangbei, ()=>{
            this.close()
            this.m_zhuangbei_cb && this.m_zhuangbei_cb(this)
        })
        this.onButtonListen(this.btn_duihuang, ()=>{
            this.m_duihuan_cb && this.m_duihuan_cb(this)
        })
        this.onButtonListen(this.btn_look_video, ()=>{
            this.m_look_video_cb && this.m_look_video_cb(this)
        })
        this.onButtonListen(this.btn_share, ()=>{
            this.m_share_cb && this.m_share_cb(this)
        })
    }

    public initView(data:car_item_data, is_jiesuo:boolean) {
        this.m_data = data
        if(is_jiesuo) {
            this.img_suo.active = false
            this.lab_car_name.active = false
            this.lab_xiangcha_score.getComponent(Label).string = data.name
            this.lab_score.getComponent(Label).string = userDataManager.user_data.license_code
        } else {
            if(data.stype==car_type.score) {
                let xiangcha_score = data.unlock_points - userDataManager.user_car_list.max_integral
                if(xiangcha_score<0) {
                    xiangcha_score = 0
                }
                this.lab_xiangcha_score.getComponent(Label).string = `距离解锁还差 ${xiangcha_score} 分`
                this.lab_score.getComponent(Label).string = data.unlock_points + '分解锁'
            } else {
                let cur_count = data.temp_bag_list_item_data.quantity
                let xiangcha_score = data.unlock_points - cur_count
                if(xiangcha_score<0) {
                    xiangcha_score = 0
                }
                this.lab_xiangcha_score.getComponent(Label).string = `距离解锁还差 ${xiangcha_score} 碎片`
                this.lab_score.getComponent(Label).string = `已集齐 ${cur_count} / ${data.unlock_points}碎片`
            }
            this.img_suo.active = true
            this.lab_car_name.active = true
            this.lab_car_name.getComponent(Label).string = data.name
        }
        
        this.img_car.getComponent(Sprite).spriteFrame = imageCacheManager.getRankCarImageById(data.id)

        this.shuxing_info.getComponent(car_info_shuxing).initView(data)

        this.operate_btn_node.active = false
        this.get_suipian_node.active = false
        if(is_jiesuo) {
            this.showOperateNode(true)
            if(userDataManager.user_car_list.default_car_id==data.id) {
                this.btn_zhuangbei.active = false
            } else {
                this.btn_zhuangbei.active = true
            }
            this.btn_duihuang.active = false
        } else {
            if(data.stype==car_type.suipian) {
                if(dataManager.userCarListHavCar(data.id)) {
                    this.showOperateNode(true)
                    this.btn_zhuangbei.active = true
                    this.btn_duihuang.active = false
                } else {
                    let cur_count = data.temp_bag_list_item_data.quantity
                    if(cur_count-data.unlock_points>=0) {
                        this.showOperateNode(true)
                        this.btn_zhuangbei.active = false
                        this.btn_duihuang.active = true
                    } else {
                        // 看视频/分享 碎片
                        this.showOperateNode(false)
                    }
                }
            }
        }
    }

    public initOperateNodeCallback(zhuangbei_cb, duihuan_cb) {
        this.m_zhuangbei_cb = zhuangbei_cb
        this.m_duihuan_cb = duihuan_cb
    }

    public initGetSuipianNodeCallback(look_video_cb, share_cb) {
        this.m_look_video_cb = look_video_cb
        this.m_share_cb = share_cb
    }

    public closeSelf() {
        this.close()
    }

    public getData():car_item_data {
        return this.m_data
    }

    private showOperateNode(is_show:boolean) {
        if(is_show) {
            this.operate_btn_node.active = true
            this.get_suipian_node.active = false
        } else {
            this.operate_btn_node.active = false
            this.get_suipian_node.active = true
        }
    }
}

