import {Injector} from "@angular/core";
import {MenuService} from "../menu/menu.service";
import {VarService} from "./var.service";

/**********************
 * 登陆初始化从服务器获取的信息
 */
export class FrameService {
  // 初始化时返回的的参数
  public static LoginInitMap: any;
  // 系统字典
  public static DictMap: any = {};
  // 系统参数
  public static ParamMap: any = {};
  // 操作员
  public static FrameOptr: any;
  // 部门信息
  public static FrameDept: any;
  // 操作员资源信息
  public static ResMap: any = {};
  // 操作员资源信息
  public static ResArray: [any];

  /*************************
   *
   * 系统登录后初始化信息
   */
  public static initializeAfterLoin(injector: Injector, map: any) {
    FrameService.LoginInitMap = map;
    // 系统字典
    for (const v of map['FrameDictArray']) {
      if (!FrameService.DictMap[v['dict_name']]) {
        FrameService.DictMap[v['dict_name']] = new Array();
      }
      FrameService.DictMap[v['dict_name']].push(v);
    }

    // 系统参数
    for (const v of map['FrameParamArray']) {
      FrameService.ParamMap[v['param_name']] = v;
    }
    // 当前登录的用户
    FrameService.FrameOptr = map['FrameOptr'];
    FrameService.FrameDept = map['FrameDept'];
    if (FrameService.FrameDept === undefined) {
      FrameService.FrameDept = {dept_id: 0, dept_level: 0, dept_name: '顶级部门'};
    }
    FrameService.FrameOptr.dept_name = FrameService.FrameDept.dept_name;
    if (FrameService.FrameOptr['optr_image'] === undefined) {
      // 如果没有图片，设置默认图片
      FrameService.FrameOptr['optr_image'] = `assets/img/user/01.jpg`;
    }

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

    // 所有菜单
    const aMenu = new Array();
    for (const v of resArray) {
      if (v['childArray'] === undefined) {
        continue;
      }
      // 添加顶层菜单
      aMenu.push({
        text: v['res_name'],
        heading: true,
        icon: v['res_css']
      });
      // 二级菜单
      for (const c of v['childArray']) {
        if (c['childArray'] && c['childArray'].length > 0) {
          const dArray = new Array();
          for (const d of c['childArray']) {
            // 三级菜单
            let submenu = [];
            if (d['childArray'] && d['childArray'].length > 0) {
              for (const f of d['childArray']) {
                submenu.push({
                  text: f['res_name'],
                  link: f['res_url'],
                  icon: f['res_css'],
                });
              }
            }
            dArray.push({
              text: d['res_name'],
              link: d['res_url'],
              icon: d['res_css'],
              submenu: submenu.length === 0 ? undefined : submenu
            });
          }
          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']
          });
        }
      }

    }

    // 设置标题
    if (FrameService.ParamMap['FrameName']) {
      document.title = FrameService.ParamMap['FrameName']['param_value'];
    }

    // 设置sessionId
    VarService.SESSION_ID = map['FrameSessionId'];

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

  /********************
   * 查找某个资源下的所有资源
   */
  public 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') {
        FrameService.findChildRes(resArray, v);
        array.push(v);
        res.childArray = array;
      }
    }
  }

  /**********************
   * 将某个res下的所有资源转为数组
   */
  public static makeResArray(res, array) {
    if (res.childArray && res.childArray.length > 0) {
      for (const c of res.childArray) {
        array.push(c);
        FrameService.makeResArray(c, array);
      }
    }
  }

  /**************************
   * 获取某一个字典所有的字典项
   * @param {string} dict_name  字典名称
   * @returns {[any]} 项目数组
   */
  public static getFrameDictArray(dict_name: string): [any] {
    return FrameService.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 && FrameService.DictMap[dict_name] !== undefined) {
      for (const v of FrameService.DictMap[dict_name]) {
        if (v.dict_value === (dict_value + '')) {
          return v;
        }
      }
    }
    return undefined;
  }

  /***********************
   * 获取参数值
   * @param {string} dict_name 字典名称
   * @param {string} dict_value 字典唯一值
   * @returns {any} 对应的字典项
   */
  public static getParamValue(param_name: string, default_value?: any) {
    if (param_name !== undefined && FrameService.ParamMap[param_name] !== undefined) {
      return FrameService.ParamMap[param_name].param_value;
    }
    if (default_value) {
      return default_value + '';
    }
    return default_value;
  }

  /******************
   * 获取字典的中文描述
   * @param {string} dict_name  字典名称
   * @param {string} dict_value 字典唯一值
   * @returns {any} 对应的描述信息
   */
  public static getFrameDictText(dict_name: string, dict_value: string) {
    const dict = FrameService.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 (FrameService.FrameOptr === undefined) {
      return false;
    }
    // 超级管理员
    if (res === 0 || res === '0' || FrameService.FrameOptr.optr_id === 0) {
      return true;
    }

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


}
