import { ComUtil } from '@global/util/ComUtil';


/**
 * 
 *
 * @export
 * @class ViewEngine
 */
export default class ViewEngine {
    /**
     * 视图部件对象
     *
     * @protected
     * @type {*}
     * @memberof ViewEngine
     */
    protected view: any = null;
    /**
     * 引擎参数
     *
     * @type {*}
     * @memberof ViewEngine
     */
    protected opt: any = {};
    /**
     *
     *
     * @type {*}
     * @memberof ViewEngine
     */
    protected methods: any = {};

    /**
     * 视图数据
     *
     * @type {*}
     * @memberof ViewEngine
     */
    public viewdata: any = {};

    /**
     * Creates an instance of ViewEngine.
     * @memberof ViewEngine
     */
    constructor() { }

    /**
     * 引擎初始化
     *
     * @param {*} [options={}]
     * @memberof GridViewEngine
     */
    public init(options: any = {}): void {
        this.opt = options;
        this.methods = options.methods;
        this.view = options.view;
        const hasviewdata: boolean = this.view.viewdata ? true : false;
        if (hasviewdata) {
            try {
                if(this.view.viewdata.parentdata){
                    let tempObj;
                    if(typeof(this.view.viewdata.parentdata)=='string'){
                        tempObj = JSON.parse(this.view.viewdata.parentdata);
                    }else{
                        tempObj = this.view.viewdata.parentdata;
                    }
                    Object.assign(this.viewdata, tempObj);
                    if (this.view.viewdata.srfkey) {
                        Object.assign(this.viewdata, { srfkey: this.view.viewdata.srfkey });
                    }
                }else{
                    if(typeof(this.view.viewdata) =='string'){
                        Object.assign(this.viewdata, JSON.parse(this.view.viewdata));
                    }else{
                        Object.assign(this.viewdata, this.view.viewdata);
                    }
                }
            } catch (error) {
                console.log(error);
            }
        } else {
            const _viewdata: any = {};
            let router = this.view.$route;
            // if (router && router.length >0) {
            //     if(router.startsWith("parentdata")){
            //         let tempRouter = JSON.parse(router.split("=")[1]).srfparentdata;
            //         if(Object.keys(tempRouter).length >0){
            //             router ="";
            //             Object.keys(tempRouter).forEach((item:any,index:any) => {
            //                 if(index < Object.keys(tempRouter).length-1){
            //                     router +=item+"="+tempRouter[item]+";";
            //                 }else{
            //                     router +=item+"="+tempRouter[item];
            //                 }
            //             });
            //         }
            //     }
            //     Object.assign(_viewdata, ComUtil.formatMatrixParse2(router));
            // }else{
            //     if(router.name){
            //         Object.assign(this.viewdata,router.name);
            //     }
            // }
            if(router && router.length >0){
                Object.assign(_viewdata, JSON.parse(router)); 
            }
            Object.assign(this.viewdata, _viewdata);
        }
        this.load();
    }

    /**
     * 设置视图数据
     *
     * @param {string} viewdata
     * @memberof ViewEngine
     */
    public setViewData(viewdata: string): void {
        this.viewdata = {};
        try {
            Object.assign(this.viewdata, JSON.parse(viewdata));
        } catch (error) {
            console.log(error);
        }

    }

    /**
     * 引擎加载
     *
     * @param {*} [opts={}]
     * @memberof ViewEngine
     */
    public load(opts: any = {}): void {

    }

    /**
     * 部件事件机制
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof ViewEngine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {

    }

    /**
     * 处理界面行为
     *
     * @param {string} tag
     * @param {string} [actionmode]
     * @memberof ViewEngine
     */
    public doSysUIAction(tag: string, actionmode?: string): void {
        if (Object.is(actionmode, 'FRONT')) {
            if (this.methods.front) {
                this.methods.front(tag);
            }
        }
    }

    /**
     * 处理工作流界面行为
     *
     * @param {string} tag
     * @param {string} [actionmode]
     * @memberof ViewEngine
     */
    public doSysWFUIAction(tag: string, actionmode?: string): void {
        if (Object.is(actionmode, 'WFFRONT')) {
            if (this.methods.wfFront) {
                this.methods.wfFront(tag);
            }
        }
    }

    /**
     * 是否为方法
     *
     * @protected
     * @param {*} func
     * @returns {boolean}
     * @memberof ViewEngine
     */
    protected isFunc(func: any): boolean {
        return func instanceof Function;
    }

    /**
     * 父数据参数模式
     *
     * @param {{ tag: string, action: string, viewdata: any }} { tag, action, viewdata }
     * @memberof ViewEngine
     */
    public setViewState2({ tag, action, viewdata }: { tag: string, action: string, viewdata: any }): void {
        this.view.viewState.next({ tag: tag, action: action, data: viewdata });
    }

}