import { _decorator, Component, Node, Tween } from 'cc';
import { gameManager } from './gameManager';
import { scene_item_data, task_data, widget_item_data } from '../../data/data';
import { scene_layer } from './scene_layer';
import { ui_layer } from './ui_layer';
import { top_layer } from './top_layer';
import { config } from '../config';
import { control } from '../edit/control';
import { loading_view } from './loading_view/loading_view';
import { login_view } from './login_view/login_view';
import { game_list } from './game_list_view/game_list';
import { check_open } from './check_open';
import { scene_page } from './scene_page';
const { ccclass, property } = _decorator;

@ccclass('game_run')
export class game_run extends Component {
    @property(Node) scene_layer:Node = null;
    @property(Node) ui_layer:Node = null;
    @property(Node) top_layer:Node = null;
    @property(Node) btn_close:Node = null;
    @property(Node) launch:Node = null;

    @property(Node) game_list:Node = null;
    @property(Node) login_view:Node = null;
    @property(Node) loading_view:Node = null;
    @property(Node) loading_level:Node = null;
    @property(Node) check_open:Node = null;
    @property(gameManager) mGameManger:gameManager = null;

    protected start(): void {
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.unInit()
            this.close()
        })
    }

    public unInit(){
        Tween.stopAll()
        this.ui_layer.getComponent(ui_layer).unInit()
        this.top_layer.getComponent(top_layer).unInit()
        this.scene_layer.getComponent(scene_layer).unInit()
    }

    public close(){
        this.node.removeFromParent()
    }

    public run(callback=null){
        this.mGameManger.init(this,control.Singleton.get_bag_data().content,callback)
    }

    public runAll(){
        this.loading_view.active = true;
        this.login_view.active = false;
        this.game_list.active = false;
        this.mGameManger.initGR(this)
        this.loading_view.getComponent(loading_view).startLoading(()=>{
            this.loading_view.active = false;
            this.showLogin()
        })
    }

    public showLogin(){
        this.login_view.active = true;
        this.login_view.getComponent(login_view).initView(()=>{
            this.showGameList(()=>{ //加载完成关闭登录界面
                this.login_view.active = false;
            })
        })
    }

    public isCurScenePageCheckWidgetFinish(widget_id:number):boolean{
       return this.scene_layer.getComponent(scene_layer).isCurScenePageCheckWidgetFinish(widget_id)
    }

    public isCurScenePageCheckWidgetShow(widget_id:number):boolean{
        return this.scene_layer.getComponent(scene_layer).isCurScenePageCheckWidgetShow(widget_id)
     }

    public showGameList(call){
        this.game_list.active = true;
        this.game_list.getComponent(game_list).initView(call,()=>{
            this.game_list.active = false;
            this.showLogin()
        })
    }

    public loadSceneTask(data:task_data) {
        this.scene_layer.getComponent(scene_layer).initSceneTask(data)
    }

    public loadSceneLayer(pages:scene_item_data[],type:number){
        this.scene_layer.getComponent(scene_layer).initScene(pages,type)
    }

    public getCurSceneLayerPage():scene_page{
        return this.scene_layer.getComponent(scene_layer).getCurScenePage()
    }

    public loadUILayer(ui_widge_list:widget_item_data[]){
        this.ui_layer.getComponent(ui_layer).initUi(ui_widge_list)
        this.initTop()
    }

    public initTop(){
        this.top_layer.getComponent(top_layer).initTop()
    }

    public loadTextSound(ui_widge_list:widget_item_data[]){
        this.top_layer.getComponent(top_layer).initTextSoundLlist(ui_widge_list)
    }

    public initQuestionList(ui_widge_list:widget_item_data[]){
        this.top_layer.getComponent(top_layer).initQuestionList(ui_widge_list)
    }


    public initCountDownList(count_down_list:widget_item_data[]){
        this.ui_layer.getComponent(ui_layer).initCountDownList(count_down_list)
    }

    public initTaskUi(data:task_data){
        this.ui_layer.getComponent(ui_layer).initTaskUi(data)
        // if(data.type===config.task_type.guo_ju_qing){
          
        // }
        this.top_layer.getComponent(top_layer).initRuleAndTips()
    }

    public showFindRuleTips(){
        this.top_layer.getComponent(top_layer).showFindRuleTips()
    }

    public showTips(){
        this.top_layer.getComponent(top_layer).showTips()
    }

    public showRule(){
        this.top_layer.getComponent(top_layer).showRule()
    }

    public showCheck(call){
        this.check_open.getComponent(check_open).show(call)
    }

    public backGameList(){
        this.unInit()
        this.onlaunch(()=>{
            this.showGameList(()=>{ 

            })
        })
    }

    public showLoadingLevel(){
        this.loading_level.active = true;
    }

    public hideLoadingLevel(){
        this.loading_level.active = false;
    }

    public loadTopLayer(){
        
    }

    public onlaunch(call,delay:number=0.7){
        this.launch.active = true;
        call()
        this.scheduleOnce(()=>{
            this.launch.active = false;
        },delay)
    }
}


