import { _decorator, Component, Director, director, instantiate, Label, Node, Prefab, ScrollView, Sprite } from 'cc';
import { base_ui } from '../fw/base_ui';
import { user_results } from '../data';
import { dataManager } from '../manager/dataManager';
import { tools } from '../tools';
import { results_item } from './results_item';
import { userDataManager } from '../manager/userDataManager';
import { uiManager } from '../manager/uiManager';
import { config } from '../config';
import { ClientEvent } from '../lib/clientEvent';
import { http } from '../http';
import { car_lib } from '../ui/car_lib/car_lib';
const { ccclass, property } = _decorator;

@ccclass('results_new_car')
export class results_new_car extends base_ui {
    @property(Node) btn_sure:Node = null
    @property(Node) btn_go_lib:Node = null
    @property(Node) btn_use_car:Node = null
    @property(Node) content:Node = null
    @property(Node) lab_fen:Node = null
    @property(Node) lab_name:Node = null
    @property(Node) icon_car:Node = null
    @property(Prefab) item_pf:Prefab = null
    @property(Node) layout_car_list:Node = null
    private default_car_id:number = -1
    // @property(Node) layout_btns:Node = null
    protected start(): void {
        this.onButtonListen(this.btn_sure,this.close.bind(this))
        this.onButtonListen(this.btn_go_lib,()=>{
            uiManager.Instance().showUi(config.UI.car_lib, this.node, (n:Node)=>{
                n.getComponent(car_lib).initGameShowStatus(this.node)
            })
        })
        this.onButtonListen(this.btn_use_car,()=>{
            if(this.default_car_id!=-1){
                userDataManager.user_car_list.default_car_id = this.default_car_id
                ClientEvent.dispatchEvent(config.UI_EVENT.UPDATE_USER_DEFAULT_CAR)
                this.syncUserSetDefaultCar()
            }
            this.default_car_id = -1
            this.close()
        })
    }
    private syncUserSetDefaultCar() {
        let opt = {'car_id': userDataManager.user_car_list.default_car_id}
        http.post(config.API.user_set_default_car_id, opt, (err,d)=>{
            let nd = JSON.parse(d)
            if(nd.code === config.status.SUCCESS){}  
        })
    }

    protected close(): void {
        this.node.active = false
    }
    public show(fen:number,data:user_results){
        if(data.obtain_list.length<=0){
            return
        }
        this.node.active = true
        this.btn_sure.active = true
        this.content.removeAllChildren()
        let new_car_d = dataManager.getUserCarInfo(data.obtain_list[0])
        if(data.obtain_list.length>1){
            this.layout_car_list.active = true
            for (let index = 1; index < data.obtain_list.length; index++) {
                let d = data.obtain_list[index];
                let item = instantiate(this.item_pf)
                item.parent = this.content
                item.getComponent(results_item).init(dataManager.getUserCarInfo(d))
            }
            this.btn_go_lib.active = true
            this.btn_use_car.active = false
            // director.once(Director.EVENT_AFTER_DRAW,()=>{
            //     this.content.parent.parent.getComponent(ScrollView).scrollToLeft(0.1)
            // })
           
        }else{
            this.default_car_id  = new_car_d.id
            this.btn_go_lib.active = false
            this.btn_use_car.active = true
            this.layout_car_list.active = false
        }
        
        tools.loadRemoteImg(new_car_d.background_cover,(data)=>{
            this.icon_car.getComponent(Sprite).spriteFrame = data.sf
        })
        this.lab_fen.getComponent(Label).string = `${fen}  分`
        this.lab_name.getComponent(Label).string = new_car_d.name
    }
}


