import { GMUserModel } from '@db/GMUser';
import { GMGroupModel } from '@db/GMGroup';
import { GMUserGroupModel } from '@db/GMUserGroup';
import { ApiModel } from '@db/Api';
import { Service } from 'egg';
import { STATUS } from '@consts';

/**
 * Test Service
 */
export default class GMUsers extends Service {

  /**
   * 后台账号登录
   */
  public async login(username: string, password: string) {
    const {ctx} = this;
    let token = ctx.service.utils.genCode(256);
    let user = await GMUserModel.login(username, password, token);

    if(!user) {
      return ctx.service.utils.resResult(STATUS.GM_ERR_PASSWORD);
    }
      let userGroups = await GMUserGroupModel.getUserGroupByUid(user.uid, 1);
      let group = new Array();
      for(let userGroup of userGroups) {
        let g = await GMGroupModel.getGroupById(userGroup.groupId);
        if(g) group.push(g.group);
      }

      return ctx.service.utils.resResult(STATUS.SUCCESS, {
        ...user, group, token
      });
  }

  /**
   * 修改我的密码
   */
  public async changeMyPass(uid: number, password: string) {
    const {ctx} = this;
    let user = ctx.user;
    if(uid != user.uid) return ctx.service.utils.resResult(STATUS.GM_NO_AUTHORITY_POST);

    let result = await GMUserModel.changePass(uid, password);
    if(result) {
      return ctx.service.utils.resResult(STATUS.SUCCESS);
    } else {
      return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
  }

  /**
   * 后台账号列表
   */
  public async getGmList() {
    // const {ctx} = this;
    let list = await GMUserModel.getGmAccountList();
    if(list) {
      let result = new Array();
      for(let user of list) {
        let uid = user.uid;
        let userGroupList = await GMUserGroupModel.getUserGroupByUid(uid, 1);
        let groupArr = new Array(), groupStrArr = new Array();
        for(let userGroup of userGroupList) {
          let group = await GMGroupModel.getGroupById(userGroup.groupId);
          console.log(userGroup.groupId, group);
          if(group) {
            groupArr.push(userGroup.groupId);
            groupStrArr.push(group.name);
          }
        }
        result.push({...user, group:groupArr,  groupStr: groupStrArr.length?groupStrArr.join():'暂无'});
      }

      return this.ctx.service.utils.resResult(STATUS.SUCCESS, {
        gmlist: result
      });

    
    } else {
      return this.ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
  }
  /**
   * 创建后台账号
   */
  public async createGmAccount(name: string, username: string, password: string) {
    const {ctx} = this;
    let token = ctx.service.utils.genCode(256);

    let user = await GMUserModel.getGmAccountByUsername(username);
    if(user) {
      return ctx.service.utils.resResult(STATUS.GM_DUPLICATE_GM_ACCOUNT)
    } else {
      user = await GMUserModel.createGmAccount(username, password, name, token);
      return ctx.service.utils.resResult(STATUS.SUCCESS);
    }
  }

  /**
   * 后台Api列表
   */
  public async getApiList() {
    const {ctx} = this;
    let list = await ApiModel.getAllApi();

    if(list) {
      return ctx.service.utils.resResult(STATUS.SUCCESS, {
        list
      });
    } else {
      return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
  }

  /**
   * 创建Api
   */
  public async saveApi(api:string, name:string) {
    const {ctx} = this;
    let result = await ApiModel.getApi(api);
    if(result) {
      return ctx.service.utils.resResult(STATUS.GM_DUPLICATE_API);
    } else {
      let result = await ApiModel.createApi(api, name);
      if(result) {
        return ctx.service.utils.resResult(STATUS.SUCCESS);
      } else {
        return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
      }
    }
  }


  /**
   * 后台用户组列表
   */
  public async getGroupList() {
    const {ctx} = this;
    let list = await GMGroupModel.getAllGroup();

    if(list) {
      return ctx.service.utils.resResult(STATUS.SUCCESS, { list });
    } else {
      return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
  }

  /**
   * 创建用户组
   */
  public async createGmGroup(group:string, name:string, desc: string) {
    const {ctx} = this;
    let result = await GMGroupModel.getGroup(group);
    if(result) {
      return ctx.service.utils.resResult(STATUS.GM_DUPLICATE_GROUP);
    } else {
      let result = await GMGroupModel.createGroup(group, name, desc);
      if(result) {
        return ctx.service.utils.resResult(STATUS.SUCCESS);
      } else {
        return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
      }
    }
  }

  /**
   * 修改用户组
   */
  public async saveGmGroup(groupId: number, group:string, name:string, desc: string) {
    const {ctx} = this;

    let result = await GMGroupModel.editGroup(groupId, group, name, desc);
    if(result) {
      return ctx.service.utils.resResult(STATUS.SUCCESS);
    } else {
      return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
  }

  /**
   * 用户组添加接口
   */
  public async saveGmApiToGroup(apiId: number, apis: Array<number>) {
    const {ctx} = this;
    let result = await GMGroupModel.saveApiToGroup(apiId, apis);
    if(result) {
      return ctx.service.utils.resResult(STATUS.SUCCESS);
    } else {
      return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
  }

  /**
   * 用户组添加接口
   */
  public async saveGmGroupToUser(uid: number, groups: Array<number>) {
    let allUserGroup = await GMUserGroupModel.getUserGroupByUid(uid, 1);

    let removeArr = allUserGroup.filter(userGroup => {
      let gid = userGroup.groupId;
      return groups.indexOf(gid) == -1;
    }).map(cur => cur.groupId);

    let flag = 0;
    let result = await GMUserGroupModel.removeUserGroup(uid, 1, removeArr);
    if(!result) {
      flag = 1; 
    }

    if(!flag) {
      for(let group of groups) {
        let result = await GMUserGroupModel.addUserGroup(uid, 1, group);
        if(!result) {
          flag = 1; break;
        } 
      }
    }

    if(flag) {
      return this.ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
    }
    return this.ctx.service.utils.resResult(STATUS.SUCCESS);

  }
}
