import { GuideVO } from "../../data/module/GuideData";

/**
 * @description 新手引导
 * @author 邹勇
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class Guide extends cc.Component {

    @property({ type: cc.Node, displayName: "显示区域" })
    node_display: cc.Node = null;
    @property({ type: cc.Node, displayName: "点击区域" })
    node_click: cc.Node = null;
    @property({ type: cc.Node, displayName: "背景" })
    node_bg: cc.Node = null;
    @property({ type: cc.Sprite, displayName: "手指" })
    node_finger: cc.Sprite = null;
    @property({ type: cc.Sprite, displayName: "头像" })
    node_head: cc.Sprite = null;
    @property({ type: cc.Sprite, displayName: "形象" })
    node_person: cc.Sprite = null;
    @property({ type: cc.RichText, displayName: "对话内容" })
    rich_dialog: cc.RichText = null;


    private guides: GuideVO[];
    private crtStep: number;
    private crtGuide: GuideVO;
    private targetNode: cc.Node;
    private event_name: string;

    onLoad() {

    }

    start() {
        if (gData.guideData.crtID == null) {
            this.node.destroy();
            return;
        }
        this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
        this.crtStep = -1;
        this.nextStep();
    }

    private nextStep() {
        this.crtStep++;
        this.crtGuide = this.guides[this.crtStep];
        this.updateGuide();
    }

    private updateGuide() {
        if (this.crtGuide == null) {
            this.close();
        }
        else {
            this.targetNode = null;
            this.event_name = null;

            //点击全屏下一步，显示区域不显示，点击区域全屏
            if (this.crtGuide.click_rect == "all") {
                this.node_display.width = 0;
                this.node_display.height = 0;
                this.node_click.x = 0;
                this.node_click.y = 0;
                this.node_click.width = this.node.width;
                this.node_click.height = this.node.height;
            }
            else if (this.crtGuide.click_rect.indexOf("event") != -1) {//点击区域下一步，并发送事件，显示区域读配置，点击区域=显示区域
                this.event_name = "event_guide_" + this.crtGuide.id + "_" + this.crtStep;
                let arr = this.crtGuide.display_rect.split(":");
                this.node_display.x = this.node_click.x = parseInt(arr[0]);
                this.node_display.y = this.node_click.y = parseInt(arr[1]);
                this.node_display.width = this.node_click.width = parseInt(arr[2]);
                this.node_display.height = this.node_click.height = parseInt(arr[3]);
            }
            else {//点击按钮下一步，显示区域为配置的node，点击区域=显示区域
                this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
                this.node_display.width = this.targetNode.width;
                this.node_display.height = this.targetNode.height;
                let pos = mk.game.getWorldPos(this.targetNode);
                this.node_display.setPosition(pos);
            }

            if (this.crtGuide.finger == "") {
                this.node_finger.node.active = false;
            }
            else {
                this.node_finger.node.active = true;
                let arr = this.crtGuide.finger.split(":");
                this.node_finger.node.x = this.node_display.x + parseInt(arr[0]);
                this.node_finger.node.y = this.node_display.y + parseInt(arr[1]);
            }

            if (this.crtGuide.tr_form == 1) {
                this.node_head.node.active = true;
                this.node_person.node.active = false;
            }
            else {
                this.node_head.node.active = false;
                this.node_person.node.active = true;
            }

            this.node_bg.x = -this.node_display.x;
            this.node_bg.y = -this.node_display.y;

            this.rich_dialog.string = `<color=#000000>${this.crtGuide.dialog}</color>`;
        }
    }

    async clickNodeClick() {
        console.log("clickNodeClick");
        if (this.targetNode) {
            this.targetNode.getComponent(cc.Button).clickEvents[0].emit([]);
        }
        if (this.event_name) {
            mk.event.emit(this.event_name);
        }
        if (this.crtGuide.lag_next > 0) {
            await mk.time.WaitForSeconds(this.crtGuide.lag_next);
        }
        this.nextStep();
    }


    private close() {
        mk.guide.close();
        this.node.destroy();
    }

}
