import { GetBuriedData } from './../ctrls/GetBuriedData';
import { GDispatcher } from './../../libs/tc/util/GDispatcher';
import { GFun } from './../../libs/tc/util/GFun';
import { NetManager } from './../../libs/tw/manager/NetManager';
import Panel from "../views/Panel";
import PanelCtrl from "../ctrls/panelCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import TipsPanel from "../tipsPanel/TipsPanel";
import Loading from '../loading/Loading';

export default class WritePanel extends Panel {


    public get skinKey() { return 'WritePanel' }
    public get groupName() { return 'write_panel' }

    public bg: eui.Image;
    public writeEndBtn: eui.Image;
    public closeBtn: eui.Group;

    private taskId
    private taskType

    constructor(data) {
        super()
        this.taskId = data.taskId
        this.taskType = data.taskType
    }

    onSkinComplete() {
        super.onSkinComplete()
        this.once(egret.Event.ADDED_TO_STAGE, this.onLoad, this);
        if (this.stage) {
            this.onLoad();
        }
    }

    private onLoad(): void {
        this.initUI();
    }

    initUI() {
        let a = [0, 1, 2, 3, 4]

        let i = this.getRandomOne(a)
        this[`writeFont${i}`].visible = true

        this.userWrite = new egret.Shape()
        this.userWrite.graphics.beginFill(0xe88315, 0);
        this.userWrite.graphics.drawRect(127, 180, 494, 258);
        this.userWrite.graphics.endFill();
        this.addChild(this.userWrite);
        this.userWrite.graphics.lineStyle(30, 0xe88315, 1);

        let exposure = GetBuriedData(24);
        NetManager.ins.showLog(exposure)
    }

    initEvents() {
        super.initEvents()
        this.bg.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onWriteBegin, this)
        this.bg.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onWriteMove, this)
        this.bg.addEventListener(egret.TouchEvent.TOUCH_END, this.onWriteEnd, this)
        this.bg.addEventListener(egret.TouchEvent.TOUCH_END, this.onWriteEnd, this)
        this.bg.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onWriteEnd, this);

        this.writeEndBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_writeEndBtn, this);
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_onClick, this);
    }


    userWrite: egret.Shape;
    startPoint: any;
    writeMark: boolean;
    private pointsArr: any[] = [];
    onWriteBegin(e) {
        this.writeMark = true
        this.userWrite.graphics.moveTo(e.stageX, e.stageY);
        this.startPoint = { x: e.stageX, y: e.stageY }
        this.pointsArr.push(this.startPoint)
    }

    onWriteMove(e) {
        if (this.writeMark) {
            this.userWrite.graphics.lineTo(e.stageX, e.stageY);
            let endPoint = { x: e.stageX, y: e.stageY }
            this.startPoint = endPoint
            this.pointsArr.push(this.startPoint)
        }
    }
    private onTap_onClick(){
        PanelCtrl.instance.btnDelay(this.closeBtn);
        this.hidePanel();
    }
    onWriteEnd(e) {
        // this.userWrite.graphics.lineTo(e.stageX, e.stageY);
        // let endPoint = { x: e.stageX, y: e.stageY }
        // this.startPoint = endPoint
        // this.pointsArr.push(this.startPoint)
        this.writeMark = false
    }

    onTap_writeEndBtn() {
        console.log(this.pointsArr);
        if (this.pointsArr.length < 1) {
            PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: "请先描写稳字，再尝试提交哦~" })
            return
        }
        let exposure = GetBuriedData(24);
        NetManager.ins.clickLog(exposure);
        this.checkPass()
    }



    removeEvents() {
        super.removeEvents()
        this.bg.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onWriteBegin, this)
        this.bg.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onWriteMove, this)
        this.bg.removeEventListener(egret.TouchEvent.TOUCH_END, this.onWriteEnd, this)
        this.bg.removeEventListener(egret.TouchEvent.TOUCH_END, this.onWriteEnd, this)
        this.bg.removeEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onWriteEnd, this)

        this.writeEndBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_writeEndBtn, this);
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_onClick, this);
    }

    // 判断是否合格
    checkPass() {
        let a = 0;
        let arr = []
        for (let i = 0; i < 38; i++) {
            for (let t = 0; t < this.pointsArr.length; t++) {
                const point = this.pointsArr[t];
                if (point.x >= this[`f${i}`].x && point.x <= this[`f${i}`].x + 50 && point.y >= this[`f${i}`].y && point.y <= this[`f${i}`].y + 50) {
                    a++
                    if (!this.arrContain(arr, this[`f${i}`])) {
                        arr.push(this[`f${i}`])
                    }
                }
            }
        }

        let condition1 = a / this.pointsArr.length > 0.4
        let condition2 = arr.length > 37 * 0.4
        if (condition1 && condition2) {
            Loading.instace.show()
            NetManager.ins.complete((s, result) => {
                Loading.instace.hide()
                if (!s) {
                    let { code } = result
                    let msg = ""
                    switch (code) {
                        case "100001":
                            msg = "请先登录~"
                            break
                        case "100101":
                            msg = "活动已结束~"
                            break
                        case "100102":
                            msg = "入参错误~"
                            break
                        case "300401":
                            msg = "任务类型错误~"
                            break
                        case "300402":
                            msg = "任务已完成，不需要再操作~"
                            break
                        default:
                            msg = "请尝试再次提交~"
                            break
                    }

                    PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: msg })
                    return
                }else{
                    PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '新增1次抽卡机会～' })
                }
                // 调用首页接口更新卡片数量

                NetManager.ins.getBaseInfo((s) => {
                    if (!s) {
                        Loading.instace.hide()
                        PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: "更新卡片失败，请稍后重试~" })
                        return
                    }
                    GDispatcher.dispatchEvent("updateCard")
                    // 调用任务信息接口更新任务
                    NetManager.ins.queryTaskList((ss) => {
                        if (!ss) {
                            Loading.instace.hide()
                            PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: "更新任务失败，请稍后重试~" })
                            return
                        }

                        Loading.instace.hide()
                        GDispatcher.dispatchEvent("updateTaskList")

                    })
                })

                this.hidePanel()

            }, this.taskId, this.taskType)
        } else {
            // 清空
            PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: "请重新描写稳字，再尝试提交哦~" })
            this.pointsArr = [];
            this.removeChild(this.userWrite)
            this.userWrite = new egret.Shape()
            this.userWrite.graphics.beginFill(0xe88315, 0);
            this.userWrite.graphics.drawRect(127, 180, 494, 258);
            this.userWrite.graphics.endFill();
            this.addChild(this.userWrite);
            this.userWrite.graphics.lineStyle(30, 0xe88315, 1);
        }
    }

    /**
     * 从数组里随机取出一个元素
     * @param e 目标数组
     * @param n 
     */
    getRandomOne(e, n?) {
        return e && "number" == typeof e.length && e.length ? e[Math.floor(Math.random() * e.length)] : ("number" != typeof n && (n = e || 1, e = 0), e + Math.random() * (n - e))
    }



    arrContain(arr: any[], e: any) {
        let mark = false;
        arr.forEach((ee) => {
            if (ee == e) {
                mark = true;
            }
        })

        return mark;
    }

}