import { NetManager } from './../../libs/tw/manager/NetManager';
import { DataManager } from './../../libs/tw/manager/DataManager';
import { HtmlTextParser } from "./HtmlTextParser";
import Panel from "../views/Panel";
import NetConst from '../NetConst';

export default class RulePanel extends Panel {
    public labContent: eui.Label;
    public scroller: eui.Scroller;
    public textstring:string = "";
    public endstr:string = "";

    start() {
        if (window['ruleTxt']) {
            this.labContent.textFlow = (new HtmlTextParser).parser(window['ruleTxt']);
            this.labContent.textColor = parseInt("fcffb4",16);
        } else {
            if (!DataManager.ins.getData("gameAjaxElement").data || !DataManager.ins.getData("gameAjaxElement").data.rule) {
                NetConst.ajaxElement(() => {
                    // DataManager
                    this.addRule();
                });
            }
            else {
                this.addRule();
            }
        }
    }

    group: eui.Group;
    private addRule() {
        const text = DataManager.ins.getData("gameAjaxElement").data.rule;
        this.textstring = text;
        this.replaceWord();
        this.labContent.textFlow = (new HtmlTextParser).parser(this.textstring);
        this.labContent.textColor = parseInt("fcffb4",16);
    }

    replaceWord(){

        if (this.textstring.search("</p>") == -1) {
            if(this.endstr){
                this.textstring = this.endstr;
            }
        } else {
            this.textstring = this.textstring.replace("</p>", "</p><br>");
            this.endstr = this.endstr.concat(this.textstring.slice(0, this.textstring.search("</p>") + 8));
            this.textstring = this.textstring.slice(this.textstring.search("</p>") + 8, this.textstring.length);
            this.replaceWord();
        }
    }
    get skinKey() { return 'Rule' }
}