import {InjectorInstance} from '../../app.module';
import {MenuService} from '../../core/menu/menu.service';
import {menu} from '../../routes/menu';
import {Injectable} from '@angular/core';

@Injectable()
export class FrameVariable {
  public static BASE_URL = 'ses-manage-v1/';
  /******************
   * 系统字典
   */
  public static DictMap: any = {};
  /*******************
   * 系统参数
   */
  public static ParamMap: any = {};
  /****************
   * 当前操作员
   */
  public static FrameOptr: any;
  /****************
   * 操作员资源信息
   */
  public static ResMap: any = {};
  /****************
   * 操作员资源信息
   */
  public static ResArray: any = [];
  /********************
   * session变量
   */
  public static FrameSessionId: string;

  public constructor(
  ) {
  }

  public static InitFrameVariable(map: any) {
    // 系统字典
    for (const v of map['FrameDictArray']) {
      if (!FrameVariable.DictMap[v.dict_name]) {
        FrameVariable.DictMap[v.dict_name] = new Array();
      }
      FrameVariable.DictMap[v.dict_name].push(v);
    }
    localStorage.setItem("DictMap",FrameVariable.DictMap);
    // 系统参数
    for (const v of map['FrameParamArray']) {
      FrameVariable.ParamMap[v.param_name] = v;
    }
    
    localStorage.setItem("ParamMap",FrameVariable.ParamMap);
    // 操作员
    FrameVariable.FrameOptr = map['FrameOptr'];
    //清空
    localStorage.setItem("FrameOptr",null);
    localStorage.setItem("login_name",null);
    localStorage.setItem("optr_org_id",null);
    localStorage.setItem("token",null);
    localStorage.setItem("sso_url",null);
    
    localStorage.setItem("FrameOptr",FrameVariable.FrameOptr);
    // let org_name = FrameVariable.FrameOptr.optr_dept;
    localStorage.setItem("login_name",FrameVariable.FrameOptr.optr_name);
    localStorage.setItem("optr_org_id",FrameVariable.FrameOptr.optr_dept);
    localStorage.setItem("token",FrameVariable.FrameOptr.token);

    if (FrameVariable.ParamMap['sso_url']) {
      localStorage.setItem("sso_url",FrameVariable.ParamMap['sso_url'].param_value);
    }else{
      localStorage.setItem("sso_url","https://zhgl.gxeea.cn:20080/login/index");
    }

    // 操作员可用资源
    const resArray = new Array();
    // 所有权限
    for (const v of map['FrameResArray']) {
      FrameVariable.ResMap['R_' + v.res_id] = v;
      if ( v.res_level === 1) {
        FrameVariable.findChildRes(map['FrameResArray'], v);
        resArray.push(v);
      }
    }
    FrameVariable.ResArray = map['FrameResArray'];
    localStorage.setItem("ResArray",FrameVariable.ResArray);

    // 所有菜单
    const aMenu = new Array();
    for (const v of resArray) {
      // 添加顶层菜单
      aMenu.push({
        text: v.res_name,
        heading: true,
        icon: v.res_css
      });
	  // 二级菜单
	  if (v.childArray && v.childArray.length > 0) {
		for (const c of v.childArray) {
			if (c.childArray && c.childArray.length > 0) {
			const dArray = new Array();
			for (const d of c.childArray) {
				dArray.push({
				text: d.res_name,
				link: d.res_url,
				icon: d.res_css
				});
			}

			aMenu.push({
				text: c.res_name,
				link: c.res_url,
				icon: c.res_css,
				submenu: dArray
			});
			} else {
			aMenu.push({
				text: c.res_name,
				link: c.res_url,
				icon: c.res_css
			});
			}
		}
		}
      document.title = '广西自考综合管理平台';

    }
    // 设置sessionId
    FrameVariable.FrameSessionId = map['FrameSessionId'];

    // 添加菜单
    const mService =  InjectorInstance.get<MenuService>(MenuService);
    
    mService.addMenu(aMenu);

    // 一级菜单
    const levelOneMenu = new Array();
    for (const v of resArray) {
      // 添加顶层菜单
      levelOneMenu.push({
        text: v.res_name,
        heading: true,
        icon: v.res_css
      });

    }

    mService.addLevelOneMenu(levelOneMenu);

    // 二级菜单
    const levelTwoMenu = new Array();
    for (const v of resArray) {
        // 二级菜单
        if (v.childArray && v.childArray.length > 0) {
        for (const c of v.childArray) {
          if (c.childArray && c.childArray.length > 0) {
              const dArray = new Array();
              for (const d of c.childArray) {
                dArray.push({
                text: d.res_name,
                link: d.res_url,
                icon: d.res_css
                });
              }

              levelTwoMenu.push({
                text: c.res_name,
                link: c.res_url,
                icon: c.res_css,
                submenu: dArray
              });
          } else {
                levelTwoMenu.push({
                text: c.res_name,
                link: c.res_url,
                icon: c.res_css
              });
          }
        }
        }

        mService.addLevelTwoMenu(levelTwoMenu);
        return;
    }
    

  }

  private static findChildRes(resArray , res) {
    const array = new Array();
    for (const v of resArray) {
      if (v.res_id !== res.res_id && v.res_pid === res.res_id && v.res_type !== 'Fun') {
        FrameVariable.findChildRes(resArray, v);
        array.push(v);
        res.childArray = array;
      }
    }
  }

  /**************************
   * 获取某一个字典所有的字典项
   * @param {string} dict_name  字典名称
   * @returns {[any]} 项目数组
   */
  public static getFrameDictArray(dict_name: string): [any] {
    // const dict = FrameVariable.DictMap[dict_name];
    return FrameVariable.DictMap[dict_name] || localStorage.getItem("DictMap")[dict_name];
    // return dict?dict:localStorage.getItem("DictMap")[dict_name];
  }
  /***********************
   * 获取字典项
   * @param {string} dict_name 字典名称
   * @param {string} dict_value 字典唯一值
   * @returns {any} 对应的字典项
   */
  public static getFrameDict(dict_name: string, dict_value: string) {
    if (dict_name !== undefined && (FrameVariable.DictMap[dict_name] !== undefined || localStorage.getItem("DictMap")[dict_name])) {
      const dict_name_Map = FrameVariable.DictMap[dict_name] || localStorage.getItem("DictMap")[dict_name];
      for (const v of dict_name_Map) {
        if (v.dict_value === (dict_value + '') ) {
          return v;
        }
      }
    }
    return undefined;
  }

  /******************
   * 获取字典的中文描述
   * @param {string} dict_name  字典名称
   * @param {string} dict_value 字典唯一值
   * @returns {any} 对应的描述信息
   */
  public static getFrameDictText(dict_name: string, dict_value: string) {
    const dict = FrameVariable.getFrameDict(dict_name, dict_value);
    if ( dict !== undefined ) {
      return dict.dict_text;
    }
    return dict_value;
  }

  /************************
   * 验证是否有权限
   * @param res 资源编号或者别名
   * @returns {boolean} 是否有对应的权限
   */
  public static validateRole(res: any): boolean {
    if (FrameVariable.FrameOptr === undefined) {
      return false;
    }
    // 超级管理员
    if (res === 0 || res === '0' || FrameVariable.FrameOptr.optr_role === 0) {
      if (FrameVariable.FrameOptr.optr_role === 0) {
        return true;
      } else {
        return false;
      }
    }

    // 一般权限判断
    for (const r of FrameVariable.ResArray) {
      if (res === res.res_id || res === r.res_alias) {
        return true;
      }
    }
    return false;
  }
}
