import { _decorator, Component, Label, Node, Sprite, utils } from 'cc';
import { base_view } from '../base_view';
import { config } from '../../config';
import { gameManager } from '../../gameManager';
import { tools } from '../../tools';
import { question } from './question';
import { fail } from '../../dialog/fail';
import { SdkUtil } from '../../framework/sdkUtil';
import { Util } from '../../framework/util';
import { StatisticsManager } from '../../framework/statisticsManager';
const { ccclass, property } = _decorator;

@ccclass('view_game_play_ji_yi_li')
export class view_game_play_ji_yi_li extends base_view {
  @property(Node) img_1:Node;
  @property(Node) question_node:Node;
  protected initUI(): void {
      
  }

  public initView(data: any, isUpdate?: boolean): void {
    this.m_data  = data;
    let self = this;
    this.categoryid = config.PLAY_TYPE.JI_YI_LI
    let levels_item = gameManager.get_cur_level_by_categoryid(this.categoryid);
    let call_back = ()=>{
      self.initAnswerView()
    }
    tools.loadRemoteImgByCategoryid(this.categoryid,levels_item.level,this.m_data.img,(sf)=>{
        self.img_1.getComponent(Sprite).spriteFrame = sf;
        call_back();
    })
    gameManager.loadPlayMusicByCategoryId(this.categoryid,()=>{
      gameManager.playGamePlayMusicByCategoryId(this.categoryid,true);
  })
  }

  showStep1(){
    this.img_1.active = true;
    this.question_node.active = false;
    this.lab_title.active = true;
  }
  showStep2(){
    this.img_1.active = false;
    this.question_node.active = true;
    this.lab_title.active = false;
    this.lab_time_count.active = false;
  }
  initAnswerView(){
    this.showStep1()
    let index = Util.getRandomInt(0,this.m_data.memory_problem.length-1);
    this.isShowWin = false;
    this.old_coin_number = gameManager.get_user_coin();
    this.lab_coin.getComponent(Label).string = Util.coinNumberToK(this.old_coin_number);
    this.lab_title.getComponent(Label).string = this.m_data.name;
    let problem = this.m_data.memory_problem[index];
    let answer = this.m_data.memory_answer[index];
    let correct_answer = this.m_data.memory_correct_answer[index];
    this.question_node.getComponent(question).initView(problem,answer,correct_answer,this.on_question_item_click.bind(this))
    this.start_time_count(this.m_data.s_time)
  }

  on_question_item_click(isRight:boolean){
    if(isRight){
      this.showWin()
    }else{
      this.showFail()
    }
  }

  protected showQuestion(): void {
    this.showStep2()
  }

  protected onFailReStartClick(fail_view: fail): void {
    gameManager.playBtnSound()
    let self = this;
    if(this.m_data.ads_restart==1){
        SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Resume_AdUnitId,(res)=>{
            if(res.isEnded){
                self.initAnswerView()
                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.initAnswerView()
    }
  }

  protected onFailQuitClick(fail_view: fail): void {
      gameManager.playBtnSound()
      fail_view.close()
      this.close()
  }

}


