import wait from "../../libs/new_tc/wait";
import SceneCtrl from "../../libs/new_wx/ctrls/sceneCtrl";
import { ModuleTypes } from "../../libs/new_wx/types/sceneTypes";
import { IQuestionData } from "../../libs/tw/data/custom/ajaxElement/IQuestionData";
import { NetName } from "../../libs/tw/enum/NetName";
import { DataManager } from "../../libs/tw/manager/DataManager";
import { NetManager } from "../../libs/tw/manager/NetManager";
import { SceneBase } from "../SceneBase";

export default class QuestionScene extends SceneBase {
    currentQuestionIndex: number;
    nextBtn: eui.Button;
    opt1: eui.Group;
    opt2: eui.Group;
    opt3: eui.Group;
    opt4: eui.Group;
    currQuestion: IQuestionData;
    score: number;
    select: any[];//[{  "id": 20,  "option": 20}]
    rightSelectCounter: number;

    start() {
        super.start();
        this.select = [];
        this.rightSelectCounter = 0;
        this.score = 0;
        this.nextBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_nextBtn, this);
        this.opt4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
        this.opt3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
        this.opt2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
        this.opt1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
        this.currentQuestionIndex = -1;
        this.nextQuestion();
    }

    onTap_nextBtn() {
        this.nextQuestion();
    }

    onTap(e: egret.TouchEvent) {
        // Loading.instace.show();
        const label = e.currentTarget;
        this.opt4.touchEnabled = false;
        this.opt3.touchEnabled = false;
        this.opt2.touchEnabled = false;
        this.opt1.touchEnabled = false;
        this.opt4.touchChildren = false;
        this.opt3.touchChildren = false;
        this.opt2.touchChildren = false;
        this.opt1.touchChildren = false;
        let i;
        for (i = 1; i <= 4; i++) {
            if (label == this['opt' + i]) {
                break;
            }
        }
        //调用接口，返回后
        NetManager.ins.checkOutAnswer((success) => {
            // Loading.instace.hide();
            if (success) {
                this.select.push({
                    id: this.currQuestion.id,
                    option: i
                });
                if (this.isRight(i)) {//选择的是对的
                    this.rightSelectCounter++;
                    this.setRight(i);
                } else {
                    this.setRight(this.getRight());
                    this.setWrong(i);
                }
                this.nextBtn.enabled = true;
                this.checkAddScore();
                this.checkGameOver();
            } else {
                this.opt4.touchEnabled = true;
                this.opt3.touchEnabled = true;
                this.opt2.touchEnabled = true;
                this.opt1.touchEnabled = true;
                this.opt4.touchChildren = true;
                this.opt3.touchChildren = true;
                this.opt2.touchChildren = true;
                this.opt1.touchChildren = true;
            }
        }, DataManager.ins.doJoinData.orderId, JSON.stringify([{ id: this.currQuestion.id, option: i }]));
        console.log(i);
    }

    setRight(i) {
        this['bg' + i].visible = false;
        this['bgyes' + i].visible = true;
        this['bgno' + i].visible = false;
    }

    setWrong(i) {
        this['bg' + i].visible = false;
        this['bgyes' + i].visible = false;
        this['bgno' + i].visible = true;
    }

    resetItem(i) {
        this['bg' + i].visible = true;
        this['bgyes' + i].visible = false;
        this['bgno' + i].visible = false;
    }

    isRight(i) {
        return DataManager.ins.getData(NetName.checkOutAnswer).data == i;
    }

    getRight() {
        return DataManager.ins.getData(NetName.checkOutAnswer).data
    }

    checkAddScore() {
        const data = DataManager.ins.getData(NetName.checkOutAnswer);
    }

    checkGameOver() {
        const len = DataManager.ins.reconAjaxElementData.questions.length;
        if (this.currentQuestionIndex >= len - 1) {
            wait(500).then(() => {
                console.log('gameover');
                if (this.rightSelectCounter >= 3)
                    SceneCtrl.instance.change(ModuleTypes.RESULT1_SCENE,
                        { result: this.select, rightSelectCounter: this.rightSelectCounter });
                else
                    SceneCtrl.instance.change(ModuleTypes.RESULT2_SCENE, this.rightSelectCounter);
            });
        }
    }

    nextQuestion() {
        this.currentQuestionIndex++;
        this.updateQuestion();
        this.nextBtn.enabled = false;
        this.opt4.touchEnabled = true;
        this.opt3.touchEnabled = true;
        this.opt2.touchEnabled = true;
        this.opt1.touchEnabled = true;
        this.opt4.touchChildren = true;
        this.opt3.touchChildren = true;
        this.opt2.touchChildren = true;
        this.opt1.touchChildren = true;
        this.resetItem(1);
        this.resetItem(2);
        this.resetItem(3);
        this.resetItem(4);
        const { questions } = DataManager.ins.reconAjaxElementData;
        this.nextBtn.visible = this.currentQuestionIndex < questions.length - 1;
    }

    updateQuestion() {
        const { questions } = DataManager.ins.reconAjaxElementData;
        const question = questions[this.currentQuestionIndex];
        this.currQuestion = question;
        this.data.order = this.currentQuestionIndex + 1;
        this.data.question = question.name;
        for (let i = 1; i <= 4; i++) {
            if (question.options[i - 1])
                this.data['opt' + i] = question.options[i - 1].option;
            else
                this.data['opt' + i] = '';
        }
    }

    afterSelectOption() {

    }

    get skinKey(): string { return 'Question' }
}