import { Environment } from '@/environments/environment';
import { UIActionTool, Util } from '@/utils';
import axios from 'axios';
import AppCenterService from "@service/app/app-center-service";
import UIService from '../ui-service';
import { Subject } from 'rxjs';
import IBIZSample0003Service from '@/service/ibizsample0003/ibizsample0003-service';
import IBIZSample0003AuthService from '@/authservice/ibizsample0003/ibizsample0003-auth-service';

/**
 * 示例实体03UI服务对象基类
 * 基于 APP/src/uiservice/%DE_PKGPATH%/%DE_PKGPATH%-ui-service-base.ts.ftl 生成
 * @export
 * @class IBIZSample0003UIServiceBase
 */
export default class IBIZSample0003UIServiceBase extends UIService {

    /**
     * 是否支持工作流
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */
    public isEnableWorkflow:boolean = false;

    /**
     * 是否支持实体主状态
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */
    public isEnableDEMainState:boolean = false;

    /**
     * 当前UI服务对应的数据服务对象
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */
    public dataService:IBIZSample0003Service = new IBIZSample0003Service();

    /**
     * 状态值
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */ 
    public stateValue: number = 0;

    /**
     * 状态属性
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */ 
    public stateField: string = "";

    /**
     * 主状态属性集合
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public mainStateFields:Array<any> = [];

    /**
     * 主状态集合Map
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public allDeMainStateMap:Map<string,string> = new Map();

    /**
     * 主状态操作标识Map
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */ 
    public allDeMainStateOPPrivsMap:Map<string,any> = new Map();

    /**
     * Creates an instance of  IBIZSample0003UIServiceBase.
     * 
     * @param {*} [opts={}]
     * @memberof  IBIZSample0003UIServiceBase
     */
    constructor(opts: any = {}) {
        super(opts);
        this.authService = new IBIZSample0003AuthService(opts);
        this.initViewMap();
        this.initDeMainStateMap();
        this.initDeMainStateOPPrivsMap();
    }

    /**
     * 初始化视图Map
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public initViewMap(){
        this.allViewMap.set(':',{viewname:'f2treeexpview',viewType:'DETREEEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-f2-tree-exp-view'});
        this.allViewMap.set(':',{viewname:'f4treeexpview',viewType:'DETREEEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-f4-tree-exp-view'});
        this.allViewMap.set(':',{viewname:'pickupgridview',viewType:'DEPICKUPGRIDVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-pickup-grid-view'});
        this.allViewMap.set(':',{viewname:'sf3editview',viewType:'DEEDITVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-sf3-edit-view'});
        this.allViewMap.set('MDATAVIEW:',{viewname:'gridview',viewType:'DEGRIDVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-grid-view'});
        this.allViewMap.set(':',{viewname:'sf1editview',viewType:'DEEDITVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-sf1-edit-view'});
        this.allViewMap.set(':',{viewname:'stabexpview_layout',viewType:'DETABEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-stab-exp-view-layout'});
        this.allViewMap.set(':',{viewname:'f3treeexpview',viewType:'DETREEEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-f3-tree-exp-view'});
        this.allViewMap.set(':',{viewname:'sf5editview',viewType:'DEEDITVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-sf5-edit-view'});
        this.allViewMap.set(':',{viewname:'spickuptreeview',viewType:'DEPICKUPTREEVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-spickup-tree-view'});
        this.allViewMap.set(':',{viewname:'spickupview2',viewType:'DEPICKUPVIEW2',srfappde:'ibizsample0003s',component:'ibizsample0003-spickup-view2'});
        this.allViewMap.set(':',{viewname:'f1treeexpview',viewType:'DETREEEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-f1-tree-exp-view'});
        this.allViewMap.set(':',{viewname:'streeexpview',viewType:'DETREEEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-stree-exp-view'});
        this.allViewMap.set(':',{viewname:'stabexpview',viewType:'DETABEXPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-stab-exp-view'});
        this.allViewMap.set(':',{viewname:'sf2editview',viewType:'DEEDITVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-sf2-edit-view'});
        this.allViewMap.set('PICKUPVIEW:',{viewname:'pickupview',viewType:'DEPICKUPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-pickup-view'});
        this.allViewMap.set(':',{viewname:'streeexpview2',viewType:'DETREEEXPVIEW2',srfappde:'ibizsample0003s',component:'ibizsample0003-stree-exp-view2'});
        this.allViewMap.set(':',{viewname:'sf4editview',viewType:'DEEDITVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-sf4-edit-view'});
        this.allViewMap.set(':',{viewname:'smpickupview2',viewType:'DEMPICKUPVIEW2',srfappde:'ibizsample0003s',component:'ibizsample0003-smpickup-view2'});
        this.allViewMap.set(':',{viewname:'editview',viewType:'DEEDITVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-edit-view'});
        this.allViewMap.set('MPICKUPVIEW:',{viewname:'mpickupview',viewType:'DEMPICKUPVIEW',srfappde:'ibizsample0003s',component:'ibizsample0003-mpickup-view'});
    }

    /**
     * 初始化主状态集合
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public initDeMainStateMap(){
    }

    /**
     * 初始化主状态操作标识
     * 
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public initDeMainStateOPPrivsMap(){
    }


    /**
     * 获取指定数据的重定向页面
     * 
     * @param srfkey 数据主键
     * @param isEnableWorkflow  重定向视图是否需要处理流程中的数据
     * @param args  {context,viewParams} 用于计算流程   
     * @memberof  IBIZSample0003UIServiceBase
     */
    public async getRDAppView(srfkey:string,isEnableWorkflow:boolean,args?:any){
        this.isEnableWorkflow = isEnableWorkflow;
        // 进行数据查询
        let result:any = await this.dataService.Get({ibizsample0003:srfkey});
        const curData:any = result.data;
        //判断当前数据模式,默认为true，todo
        const iRealDEModel:boolean = true;
        let bDataInWF:boolean = false;
		let bWFMode:any = false;
		if (this.isEnableWorkflow) {
            return this.getWFView();
        } else {
            if ((args && args.viewParam && args.viewParam.srfwf && this.InWorkflowArray.indexOf(args.viewParam.srfwf) !== -1)) {
                return this.getWFView();
            }
        }
        let strPDTViewParam:string = await this.getDESDDEViewPDTParam(curData, bDataInWF, bWFMode);
        return this.allViewMap.get(strPDTViewParam);
    }

    /**
	 * 获取实际的数据类型
     * 
     * @memberof  IBIZSample0003UIServiceBase
	 */
	public getRealDEType(entity:any){

    }

    /**
     * 获取实体单数据实体视图预定义参数
     * 
     * @param curData 当前数据
     * @param bDataInWF 是否有数据在工作流中
     * @param bWFMode   是否工作流模式
     * @memberof  IBIZSample0003UIServiceBase
     */
    public async getDESDDEViewPDTParam(curData:any, bDataInWF:boolean, bWFMode:boolean){
        let strPDTParam:string = '';
		if (bDataInWF) {
			// 判断数据是否在流程中
        }
        //多表单，todo
        const multiFormDEField:string|null =null;

        if (multiFormDEField) {
			const objFormValue:string = curData[multiFormDEField];
			if(!Environment.isAppMode){
				return 'MOBEDITVIEW:'+objFormValue;
			}
			return 'EDITVIEW:'+objFormValue;
        }
		if(!Environment.isAppMode){
            if(this.getDEMainStateTag(curData)){
                return `MOBEDITVIEW:MSTAG:${ this.getDEMainStateTag(curData)}`;
            }
			return 'MOBEDITVIEW:';
        }
        if(this.getDEMainStateTag(curData)){
            return `EDITVIEW:MSTAG:${ this.getDEMainStateTag(curData)}`;
        }
		return 'EDITVIEW:';
    }

    /**
     * 获取数据对象的主状态标识
     * 
     * @param curData 当前数据
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public getDEMainStateTag(curData:any){
        if(this.mainStateFields.length === 0) return null;

        this.mainStateFields.forEach((singleMainField:any) =>{
            if(!(singleMainField in curData)){
                console.warn(`当前数据对象不包含属性「${singleMainField}」，根据「${singleMainField}」属性进行的主状态计算默认为空值`);
            }
        })
        for (let i = 0; i <= 1; i++) {
            let strTag:string = (curData[this.mainStateFields[0]] != null && curData[this.mainStateFields[0]] !== "")?(i == 0) ? `${curData[this.mainStateFields[0]]}` : "":"";
            if (this.mainStateFields.length >= 2) {
                for (let j = 0; j <= 1; j++) {
                    let strTag2:string = (curData[this.mainStateFields[1]] != null && curData[this.mainStateFields[1]] !== "")?`${strTag}__${(j == 0) ? `${curData[this.mainStateFields[1]]}` : ""}`:strTag;
                    if (this.mainStateFields.length >= 3) {
                        for (let k = 0; k <= 1; k++) {
                            let strTag3:string = (curData[this.mainStateFields[2]] != null && curData[this.mainStateFields[2]] !== "")?`${strTag2}__${(k == 0) ? `${curData[this.mainStateFields[2]]}` : ""}`:strTag2;
                            // 判断是否存在
                            return this.allDeMainStateMap.get(strTag3);
                        }
                    }else{
                        return this.allDeMainStateMap.get(strTag2);
                    }
                }
            }else{
                return this.allDeMainStateMap.get(strTag);
            }
        }
        return null;
    }

    /**
     * 获取数据对象当前操作标识
     * 
     * @param data 当前数据
     * @memberof  IBIZSample0003UIServiceBase
     */  
    public getDEMainStateOPPrivs(data:any){
        if(this.getDEMainStateTag(data)){
            return this.allDeMainStateOPPrivsMap.get((this.getDEMainStateTag(data) as string));
        }else{
            return null;
        }
    }

    /**
     * 获取数据对象所有的操作标识
     * 
     * @param data 当前数据
     * @memberof  IBIZSample0003UIServiceBase
     */ 
    public getAllOPPrivs(data:any){
       return this.authService.getOPPrivs(this.getDEMainStateOPPrivs(data));
    }

    /**
     * 执行界面逻辑
     *
     * @param {string} name
     * @param {any[]} args
     * @param {*} [context={}]
     * @param {*} [params={}]
     * @param {*} [$event]
     * @param {*} [xData]
     * @param {*} [actionContext]
     * @param {string} [srfParentDeName]
     * @memberof UIService
     */
    public async executeUILogic(name: string, args: any[], context?: any, params?: any, $event?: any, xData?: any, actionContext?: any, srfParentDeName?: string | undefined): Promise<any> {
    } 

}