import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { http } from '../http';
import { config } from '../config';
import { audioManager } from '../manager/audioManager';
import { user_results } from '../data';
import { ClientEvent } from '../lib/clientEvent';
import { results_unlock_car } from './results_unlock_car';
import { base_ui } from '../fw/base_ui';
import { GameManager } from '../GameManager';
import { dataManager } from '../manager/dataManager';
import { results_new_car } from './results_new_car';
const { ccclass, property } = _decorator;

@ccclass('results')
export class results extends base_ui {
    @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
    @property(Node) spr_title:Node = null
    @property(results_new_car) res_new_car:results_new_car = null
    private call_back = null
    private back_home_cb = null
    @property(SpriteFrame) TITLE_SF_LIST:SpriteFrame[] = []
    start() {
        this.onButtonListen(this.btn_restart,()=>{
            if(this.call_back!=null){
                GameManager.checkPlayGame(this.node,()=>{
                    this.call_back(false)
                    this.close()
                })
            }
            // this.onClickRestart()
        })
        this.onButtonListen(this.btn_back_home,()=>{
            if(this.back_home_cb){
                this.back_home_cb()
            }
        })
        this.onButtonListen(this.btn_video_reLife,()=>{
            GameManager.showVideoAd(config.ADS_TYPE.GAME_RELIFE_VIDEO, ()=>{
                if(this.call_back){
                    this.call_back(true)
                    this.close()
                }
            })
        })

        this.onButtonListen(this.btn_share,()=>{
            GameManager.gameOverResultsShare()
         })
    }

    protected close(): void {
        this.unlock_car.getComponent(results_unlock_car).hide()
        this.node.active = false;
    }

    setTitleSpr(scores:number){
       let index = dataManager.getGameEndPrompt(scores)
       if(index!=-1){
            this.spr_title.getComponent(Sprite).spriteFrame = this.TITLE_SF_LIST[index]
       }else{
            this.spr_title.getComponent(Sprite).spriteFrame = null
       }
    }

    public show(scores:number,jiesuan_finish_cb,call,back_home_cb,isReLife:boolean){
        this.back_home_cb = back_home_cb
        this.btn_video_reLife.active = isReLife
        this.setTitleSpr(scores)
        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)
                        ClientEvent.dispatchEvent(config.UI_EVENT.GET_NEW_CAR,res)
                        this.res_new_car.show(scores,res)
                    }
                    this.lab_des.getComponent(Label).string=`已超过全国${res.ratio}的司机`
                    ClientEvent.dispatchEvent(config.UI_EVENT.GAME_OVER_SETTLE_ACCOUNT,res)
                }
                // 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)
                // })
            }
            jiesuan_finish_cb()
        })
        audioManager.Instance().playSound(config.AUDIO.win)
        this.node.active = true
        this.lab_scores.getComponent(Label).string =scores+""
        this.call_back = call
    }
}
