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 IBIZCustomerService from '@/service/ibizcustomer/ibizcustomer-service';
import IBIZCustomerAuthService from '@/authservice/ibizcustomer/ibizcustomer-auth-service';
import Test_buttonUILogic from './test-button-ui-logic';

/**
 * 客户UI服务对象基类
 * 基于 APP/src/uiservice/%DE_PKGPATH%/%DE_PKGPATH%-ui-service-base.ts.ftl 生成
 * @export
 * @class IBIZCustomerUIServiceBase
 */
export default class IBIZCustomerUIServiceBase extends UIService {

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

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

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

    /**
     * 所有关联视图
     * 
     * @memberof  IBIZCustomerUIServiceBase
     */ 
    public allViewMap: Map<string, Object> = new Map();

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

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

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

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

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

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

    /**
     * 初始化视图Map
     * 
     * @memberof  IBIZCustomerUIServiceBase
     */  
    public initViewMap(){
        this.allViewMap.set(':',{viewname:'usr2gridview',srfappde:'ibizcustomers',component:'ibizcustomer-usr2-grid-view'});
        this.allViewMap.set(':',{viewname:'pickupgridview',srfappde:'ibizcustomers',component:'ibizcustomer-pickup-grid-view'});
        this.allViewMap.set(':',{viewname:'pickuptreeview_layout',srfappde:'ibizcustomers',component:'ibizcustomer-pickup-tree-view-layout'});
        this.allViewMap.set(':',{viewname:'usr2editview',srfappde:'ibizcustomers',component:'ibizcustomer-usr2-edit-view'});
        this.allViewMap.set(':',{viewname:'usr2pickupview',srfappde:'ibizcustomers',component:'ibizcustomer-usr2-pickup-view'});
        this.allViewMap.set(':',{viewname:'usr2mpickupview',srfappde:'ibizcustomers',component:'ibizcustomer-usr2-mpickup-view'});
        this.allViewMap.set(':',{viewname:'mpickupview_layout',srfappde:'ibizcustomers',component:'ibizcustomer-mpickup-view-layout'});
        this.allViewMap.set(':',{viewname:'editview',srfappde:'ibizcustomers',component:'ibizcustomer-edit-view'});
        this.allViewMap.set(':',{viewname:'pickuptreeview',srfappde:'ibizcustomers',component:'ibizcustomer-pickup-tree-view'});
        this.allViewMap.set(':',{viewname:'mediatesteditview',srfappde:'ibizcustomers',component:'ibizcustomer-media-test-edit-view'});
        this.allViewMap.set(':',{viewname:'usr2mpickupview_layout',srfappde:'ibizcustomers',component:'ibizcustomer-usr2-mpickup-view-layout'});
        this.allViewMap.set(':',{viewname:'usr3editview',srfappde:'ibizcustomers',component:'ibizcustomer-usr3-edit-view'});
        this.allViewMap.set(':',{viewname:'pickupview_layout',srfappde:'ibizcustomers',component:'ibizcustomer-pickup-view-layout'});
        this.allViewMap.set('MPICKUPVIEW:',{viewname:'mpickupview',srfappde:'ibizcustomers',component:'ibizcustomer-mpickup-view'});
        this.allViewMap.set(':',{viewname:'usr2pickupview_layout',srfappde:'ibizcustomers',component:'ibizcustomer-usr2-pickup-view-layout'});
        this.allViewMap.set('PICKUPVIEW:',{viewname:'pickupview',srfappde:'ibizcustomers',component:'ibizcustomer-pickup-view'});
        this.allViewMap.set('MDATAVIEW:',{viewname:'gridview',srfappde:'ibizcustomers',component:'ibizcustomer-grid-view'});
    }

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

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

        /**
     * 应用视图
     *
     * @param {any[]} args 当前数据
     * @param {any} context 行为附加上下文
     * @param {*} [params] 附加参数
     * @param {*} [$event] 事件源
     * @param {*} [xData]  执行行为所需当前部件
     * @param {*} [actionContext]  执行行为上下文
     * @param {*} [srfParentDeName] 父实体名称
     * @returns {Promise<any>}
     * @memberof IBIZCustomerUIService
     */
    public async IBIZCustomer_panel_Layoutpanel_button_calluilogic18_click(args: any[], context:any = {}, params: any={}, $event?: any, xData?: any, actionContext?: any, srfParentDeName?:string) {
        let data: any = {};
        let tempData: any = {};
        let tempContext: any = {};
        let tempViewParam: any = {};
        const _this: any = actionContext;
        const _args: any[] = Util.deepCopy(args);
        const actionTarget: string | null = 'SINGLEDATA';
        tempContext = UIActionTool.handleContextParam(actionTarget, _args, context, params, tempContext);
        tempData = UIActionTool.handleActionParam(actionTarget, _args, context, params, tempViewParam);
        Object.assign(data, tempData);
        Object.assign(context, tempContext);
        Object.assign(params, data);
        let parentObj:any = {
            srfparentdename: srfParentDeName ? srfParentDeName : null,
            srfparentkey: srfParentDeName ? context[srfParentDeName.toLowerCase()] : null
        };
        Object.assign(context, parentObj);
        let deResParameters: any[] = [];
        const parameters: any[] = [
            { pathName: 'ibizappviews', parameterName: 'ibizappview' },
            { pathName: 'listview', parameterName: 'listview' },
        ];
        const openIndexViewTab = (data: any) => {
            const routePath = actionContext.$viewTool.buildUpRoutePath(actionContext.$route, context, deResParameters, parameters, _args, data);
            actionContext.$router.push(routePath);
            //  后续界面行为
            return { ok: true, result: args };
        }
        return openIndexViewTab(data);
    }

        /**
     * 图书
     *
     * @param {any[]} args 当前数据
     * @param {any} context 行为附加上下文
     * @param {*} [params] 附加参数
     * @param {*} [$event] 事件源
     * @param {*} [xData]  执行行为所需当前部件
     * @param {*} [actionContext]  执行行为上下文
     * @param {*} [srfParentDeName] 父实体名称
     * @returns {Promise<any>}
     * @memberof IBIZCustomerUIService
     */
    public async IBIZCustomer_panel_Layoutpanel_button_calluilogic19_click(args: any[], context:any = {}, params: any={}, $event?: any, xData?: any, actionContext?: any, srfParentDeName?:string) {
        let data: any = {};
        let tempData: any = {};
        let tempContext: any = {};
        let tempViewParam: any = {};
        const _this: any = actionContext;
        const _args: any[] = Util.deepCopy(args);
        const actionTarget: string | null = 'SINGLEDATA';
        tempContext = UIActionTool.handleContextParam(actionTarget, _args, context, params, tempContext);
        tempData = UIActionTool.handleActionParam(actionTarget, _args, context, params, tempViewParam);
        Object.assign(data, tempData);
        Object.assign(context, tempContext);
        Object.assign(params, data);
        let parentObj:any = {
            srfparentdename: srfParentDeName ? srfParentDeName : null,
            srfparentkey: srfParentDeName ? context[srfParentDeName.toLowerCase()] : null
        };
        Object.assign(context, parentObj);
        let deResParameters: any[] = [];
        const parameters: any[] = [
            { pathName: 'ibizbooks', parameterName: 'ibizbook' },
            { pathName: 'treeview', parameterName: 'treeview' },
        ];
        const openIndexViewTab = (data: any) => {
            const routePath = actionContext.$viewTool.buildUpRoutePath(actionContext.$route, context, deResParameters, parameters, _args, data);
            actionContext.$router.push(routePath);
            //  后续界面行为
            return { ok: true, result: args };
        }
        return openIndexViewTab(data);
    }

        /**
     * 图书
     *
     * @param {any[]} args 当前数据
     * @param {any} context 行为附加上下文
     * @param {*} [params] 附加参数
     * @param {*} [$event] 事件源
     * @param {*} [xData]  执行行为所需当前部件
     * @param {*} [actionContext]  执行行为上下文
     * @param {*} [srfParentDeName] 父实体名称
     * @returns {Promise<any>}
     * @memberof IBIZCustomerUIService
     */
    public async IBIZCustomer_panel_Layoutpanel_button_calluilogic20_click(args: any[], context:any = {}, params: any={}, $event?: any, xData?: any, actionContext?: any, srfParentDeName?:string) {
        let data: any = {};
        let tempData: any = {};
        let tempContext: any = {};
        let tempViewParam: any = {};
        const _this: any = actionContext;
        const _args: any[] = Util.deepCopy(args);
        const actionTarget: string | null = 'SINGLEDATA';
        tempContext = UIActionTool.handleContextParam(actionTarget, _args, context, params, tempContext);
        tempData = UIActionTool.handleActionParam(actionTarget, _args, context, params, tempViewParam);
        Object.assign(data, tempData);
        Object.assign(context, tempContext);
        Object.assign(params, data);
        let parentObj:any = {
            srfparentdename: srfParentDeName ? srfParentDeName : null,
            srfparentkey: srfParentDeName ? context[srfParentDeName.toLowerCase()] : null
        };
        Object.assign(context, parentObj);
        let deResParameters: any[] = [];
        const parameters: any[] = [
            { pathName: 'ibizbooks', parameterName: 'ibizbook' },
            { pathName: 'treeview', parameterName: 'treeview' },
        ];
        const openIndexViewTab = (data: any) => {
            const routePath = actionContext.$viewTool.buildUpRoutePath(actionContext.$route, context, deResParameters, parameters, _args, data);
            actionContext.$router.push(routePath);
            //  后续界面行为
            return { ok: true, result: args };
        }
        return openIndexViewTab(data);
    }

        /**
     * 打开链接
     *
     * @param {any[]} args 当前数据
     * @param {any} context 行为附加上下文
     * @param {*} [params] 附加参数
     * @param {*} [$event] 事件源
     * @param {*} [xData]  执行行为所需当前部件
     * @param {*} [actionContext]  执行行为上下文
     * @param {*} [srfParentDeName] 父实体名称
     * @returns {Promise<any>}
     * @memberof IBIZCustomerUIService
     */
    public async IBIZCustomer_panel_Layoutpanel_button_calluilogic21_click(args: any[], context:any = {}, params: any={}, $event?: any, xData?: any, actionContext?: any, srfParentDeName?:string) {
        let data: any = {};
        let tempData: any = {};
        let tempContext: any = {};
        let tempViewParam: any = {};
        const _this: any = actionContext;
        const _args: any[] = Util.deepCopy(args);
        const actionTarget: string | null = 'SINGLEDATA';
        tempContext = UIActionTool.handleContextParam(actionTarget, _args, context, params, tempContext);
        tempData = UIActionTool.handleActionParam(actionTarget, _args, context, params, tempViewParam);
        Object.assign(data, tempData);
        Object.assign(context, tempContext);
        Object.assign(params, data);
        let parentObj:any = {
            srfparentdename: srfParentDeName ? srfParentDeName : null,
            srfparentkey: srfParentDeName ? context[srfParentDeName.toLowerCase()] : null
        };
        Object.assign(context, parentObj);
        const openPopupApp = (url: string) => {
            window.open(url, '_blank');
            return { ok: true, result: _args };
        }
        const url = `https://www.baidu.com/`;
        return openPopupApp(url);
    }


    /**
     * 获取指定数据的重定向页面
     * 
     * @param srfkey 数据主键
     * @param isEnableWorkflow  重定向视图是否需要处理流程中的数据
     * @memberof  IBIZCustomerUIServiceBase
     */
    public async getRDAppView(srfkey:string,isEnableWorkflow:boolean){
        this.isEnableWorkflow = isEnableWorkflow;
        // 进行数据查询
        let result:any = await this.dataService.Get({ibizcustomer:srfkey});
        const curData:any = result.data;
        //判断当前数据模式,默认为true，todo
        const iRealDEModel:boolean = true;

        let bDataInWF:boolean = false;
		let bWFMode:any = false;
		// 计算数据模式
		if (this.isEnableWorkflow) {
			bDataInWF = await this.dataService.testDataInWF({stateValue:this.stateValue,stateField:this.stateField},curData);
			if (bDataInWF) {
				bDataInWF = true;
				bWFMode = await this.dataService.testUserExistWorklist(null,curData);
			}
        }
        let strPDTViewParam:string = await this.getDESDDEViewPDTParam(curData, bDataInWF, bWFMode);
        //若不是当前数据模式，处理strPDTViewParam，todo

        //查找视图

        //返回视图
        return this.allViewMap.get(strPDTViewParam);
    }

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

    }

    /**
     * 获取实体单数据实体视图预定义参数
     * 
     * @param curData 当前数据
     * @param bDataInWF 是否有数据在工作流中
     * @param bWFMode   是否工作流模式
     * @memberof  IBIZCustomerUIServiceBase
     */
    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  IBIZCustomerUIServiceBase
     */  
    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  IBIZCustomerUIServiceBase
     */  
    public getDEMainStateOPPrivs(data:any){
        if(this.getDEMainStateTag(data)){
            return this.allDeMainStateOPPrivsMap.get((this.getDEMainStateTag(data) as string));
        }else{
            return null;
        }
    }

    /**
     * 获取数据对象所有的操作标识
     * 
     * @param data 当前数据
     * @memberof  IBIZCustomerUIServiceBase
     */ 
    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> {
        if (name === 'test_button') {
            return await Test_buttonUILogic.getInstance().execute(args, context, params, $event, xData, actionContext, srfParentDeName);
        }
    } 

}