import { _decorator, Component, instantiate, Label, log, Node, Prefab, SpriteFrame, UITransform, Vec3 } from 'cc';
import { base_view } from '../base_view';
import { tools } from '../../tools';
import { gameManager } from '../../gameManager';
import { diff_item } from './diff_item';
import { diff_child_item } from './diff_child_item';
import { config } from '../../config';
import { SdkUtil } from '../../framework/sdkUtil';
import { fail } from '../../dialog/fail';
import { Util } from '../../framework/util';
import { StatisticsManager } from '../../framework/statisticsManager';
const { ccclass, property } = _decorator;

@ccclass('view_game_play_zhao_bu_tong')
export class view_game_play_zhao_bu_tong extends base_view {
    public static map:Map<string,string> = new Map();
    @property(Node) grid_list:Node;
    @property(Prefab) item_prefab;
    public static sfList:Map<string,SpriteFrame> = new Map;
    protected initUI(): void {
        view_game_play_zhao_bu_tong.map.clear()
    }
    public initView(data: any, isUpdate?: boolean): void {
        this.m_data = data;
        this.categoryid = config.PLAY_TYPE.ZHAO_BU_TONG;
        let self = this;
        this.isShowWin = false;

        let list = this.m_data.reward.split(",");
        let start = parseInt(list[0]);
        let end = parseInt(list[1]);
        this.reward =  Util.getRandomInt(start,end);

        let call_back = ()=>{
            if(count>=this.m_data.img_list.length+1){
                gameManager.loadPlayMusicByCategoryId(this.categoryid,()=>{
                    self.node.active = true;
                    gameManager.hideLoading();
                    self.initGridList()
                    gameManager.playGamePlayMusicByCategoryId(this.categoryid,true);
                })
            }
        }

        let levels_item = gameManager.get_cur_level_by_categoryid(this.categoryid);
        let play_item = gameManager.get_play_list_item_by_categoryid(this.categoryid);
        let count = 0;
        tools.loadRemoteImgByCategoryid(this.categoryid,levels_item.level,this.m_data.img_answer,(sf)=>{
            count++;
            view_game_play_zhao_bu_tong.sfList.set("100" ,sf);
            call_back();
        })
        for (let index = 0; index <  this.m_data.img_list.length; index++) {
            view_game_play_zhao_bu_tong.map.set(this.m_data.img_list[index].id+"",this.m_data.img_list[index].img)
            tools.loadRemoteImgByCategoryid(this.categoryid,levels_item.level,this.m_data.img_list[index].img,(sf,id)=>{
                count++;
                console.log("loadRemoteImgByCategoryid",this.m_data.img_list[index].id)
                view_game_play_zhao_bu_tong.sfList.set(this.m_data.img_list[index].id+"" ,sf);
                call_back();
            },this.m_data.img_list[index].id);
            
        }
        console.log("view_game_play_zhao_bu_tong",this.m_data)
    }
    onGridItemClick(_diff_child_item:diff_child_item){
       if(_diff_child_item.isRight()){
            gameManager.playRightSoundByCategoryId(this.categoryid);
            _diff_child_item.Finish()
            _diff_child_item.show()
            this.hideGuid()
            if(!this.isShowWin){
                this.stop_time_count();
                if(gameManager.isFinishAllLevel(this.categoryid)){
                    tools.showToast("当前已是最后一关，敬请期待最新版本");
                }else{
                    let self = this;
                    let item = this.getTipItem()
                    item.show()
                    self.scheduleOnce(()=>{
                        item.blinkShow()
                        this.showWin();
                    },2)
                }
    
            }

       }else{
            gameManager.playErrSoundByCategoryId(this.categoryid);;
            _diff_child_item.showErrStatus()
            this.error_sub_time()
            this.showErrLab(_diff_child_item.node)
       }

    }

    getTipItem():diff_child_item{
        let self = this;
        for (let index = 0; index < self.grid_list.children.length; index++) {
            const _diff_item:Node = self.grid_list.children[index];
            for (let index = 0; index < _diff_item.children.length; index++) {
                const element = _diff_item.children[index];
                if(element.getComponent(diff_child_item).isRight()){
                    return element.getComponent(diff_child_item);
                }
            }
      
        }
        return null;
    }

    initGridList(){
        let isOffset = false;
        this.grid_list.removeAllChildren()
        this.old_coin_number = gameManager.get_user_coin();
        this.lab_coin.getComponent(Label).string = Util.coinNumberToK(this.old_coin_number);
        let list:any[] =JSON.parse(this.m_data.content);
        this.lab_title.getComponent(Label).string = this.m_data.name;
        for (let index = 0; index < list.length; index++) {
            const item_list:[] = list[index];
            let item:Node = instantiate(this.item_prefab)
            item.parent = this.grid_list;
            item.getComponent(diff_item).initView(isOffset,item_list.length,this.onGridItemClick.bind(this),item_list)
            isOffset = !isOffset;
        }
        if(this.checkShowGuid()){
            this.scheduleOnce(()=>{
                let item  = this.getTipItem();
                let v3 = item.node.position;
                let new_pos =  item.node.parent.getComponent(UITransform).convertToWorldSpaceAR(v3)
                this.showGuid(new_pos,()=>{
                },item.getComponent(UITransform).contentSize)
            },0.5)
        }else{
            this.start_time_count(this.m_data.s_time)
        }
    }

    onTipBtnClick(){
        let self  = this;
        let tip_call = ()=>{
            let item = this.getTipItem();
            if(!item){
                return;
            }
            item.blinkShow();
        }
        if(gameManager.isTestUser()){
            tip_call();
        }else{
            SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Tip_AdUnitId,(res)=>{
                if(res.isEnded){
                    tip_call();
                }
                // 统计-激励视频广告
                let collect_data = StatisticsManager.get_collect_data(this.categoryid, this.m_data.id, 0, res, config.STATISTICS_ACTION_TYPE.TI_SHI)
                StatisticsManager.request_collect_rewardVideoData(collect_data)
            })
        }
    
    }

    onFailReStartClick(fail_view:fail){
        gameManager.playBtnSound()
        let self = this;
        if(this.m_data.ads_restart==1){
            SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Resume_AdUnitId,(res)=>{
                if(res.isEnded){
                    self.initGridList()
                    fail_view.close()
                }
                // 统计-激励视频广告
                let collect_data = StatisticsManager.get_collect_data(this.categoryid, this.m_data.id, 0, res, config.STATISTICS_ACTION_TYPE.CHONG_XIN_KAI_SHI)
                StatisticsManager.request_collect_rewardVideoData(collect_data)
            })
        }else{
            fail_view.close()
            self.initGridList()
        }
    }
}


