import { _decorator, Component, Label, Node } from 'cc';
import { http } from '../http';
import { config } from '../config';
import { audioManager } from '../manager/audioManager';
import { uiManager } from '../manager/uiManager';
import { user_results } from '../data';
import { userDataManager } from '../manager/userDataManager';
import { ClientEvent } from '../lib/clientEvent';
import { SdkUtil } from '../sdkUtil';
import { results_unlock_car } from './results_unlock_car';
const { ccclass, property } = _decorator;

@ccclass('results')
export class results extends Component {
    @property(Node) btn_restart:Node = null
    @property(Node) lab_scores:Node = null
    @property(Node) btn_back_home:Node = null
    @property(Node) btn_video_reLife:Node = null
    @property(Node) unlock_car:Node = null
    @property(Node) btn_share:Node = null
    @property(Node) lab_des:Node = null
    private call_back = null
    private back_home_cb = null
    start() {
        uiManager.Instance().onButtonListen(this.btn_restart,()=>{
            if(this.call_back!=null){
                this.call_back()
                this.close()
            }
        })
        uiManager.Instance().onButtonListen(this.btn_back_home,()=>{
           if(this.back_home_cb){
                this.back_home_cb()
           }
        })


        uiManager.Instance().onButtonListen(this.btn_video_reLife,()=>{
            SdkUtil.showVideoAd(config.ADS_CONFIG.GAME_RELIFE_VIDEO,()=>{
                if(this.call_back){
                    this.call_back(true)
                    this.close()
                }
            })
        })

         uiManager.Instance().onButtonListen(this.btn_share,()=>{
            SdkUtil.shareGame(config.ADS_CONFIG.GAME_RELIFE_SHARE_TITLE,()=>{
                // if(this.call_back){
                //     this.call_back(true)
                //     this.close()
                // }
            })
         })
    }

    private close(){
        this.node.active = false;
    }


    public show(scores:number,call,back_home_cb,isFristOverGame:boolean){
        this.back_home_cb = back_home_cb
        this.btn_video_reLife.active = isFristOverGame
        http.post(config.API.sync_integral,{"integral":scores},(err,data)=>{
            if(!err){
                let d = JSON.parse(data)

                if(d.code===config.status.SUCCESS){
                    let res:user_results = d.content
                    if(res.obtain_list.length<=0){
                        this.unlock_car.getComponent(results_unlock_car).hide()
                        console.log("啥也没获取到")
                    }else{
                        this.unlock_car.getComponent(results_unlock_car).show(res.default_car_id)
                        userDataManager.user_car_list.default_car_id = res.default_car_id
                        ClientEvent.dispatchEvent(config.UI_EVENT.GET_NEW_CAR)
                    }
                    this.lab_des.getComponent(Label).string=`已超过全国${res.ratio}的司机`
                }
                // http.get(config.STATIC_API.regions,(e,d)=>{
                //     console.log("regions",d)
                // })

                // http.get(config.STATIC_API.rankings(0),(err,data)=>{
                //     console.log("rankings",data)
                // })
            }
        })
        audioManager.Instance().playSound(config.AUDIO.win)
        this.node.active = true
        this.lab_scores.getComponent(Label).string =scores+""
        this.call_back = call
    }
}
