package com.microservices.pms.enterprise.domain.vo;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.enums.SystemRole;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.common.core.utils.html.EscapeUtil;
import com.microservices.system.api.domain.SimpleSysRole;
import com.microservices.system.api.domain.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * GitLink用户简单信息对象
 * 
 * @author otto
 * @date 2023-10-10
 */
@Data
@ApiModel("GitLink用户简单信息对象")
public class GitLinkUserSimpleInfoVo
{
    public GitLinkUserSimpleInfoVo() {}

    public GitLinkUserSimpleInfoVo(JSONObject orgUser, String gitLinkUrl) {
        JSONObject user = orgUser.getJSONObject("user");
        this.username = user.getString("login");
        this.userId = user.getLong("user_id");
        this.imageUrl = EscapeUtil.removeExtraSlashOfUrl(gitLinkUrl+"/"+user.getString("image_url"));
        this.nickname = user.getString("name");
        this.email = user.getString("mail");
    }

    public GitLinkUserSimpleInfoVo(SysUser sysUser) {
        this.username = sysUser.getUserName();
        this.userId = sysUser.getGitlinkUserId();
        this.imageUrl = sysUser.getAvatar();
        this.nickname = sysUser.getNickName();
        this.email = sysUser.getEmail();
    }

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "用户ID")
    private Long userId;

    @ApiModelProperty(value = "用户头像")
    private String imageUrl;

    @ApiModelProperty(value = "用户昵称")
    private String nickname;

    @ApiModelProperty(value = "用户邮箱")
    private String email;

    public GitLinkUserToEnterpriseVo toGitLinkUserToEnterpriseInputVo(String authorize) {
        GitLinkUserToEnterpriseVo target = new GitLinkUserToEnterpriseVo();
        String roleName, roleKey;
        BeanUtils.copyProperties(this, target);
        switch (authorize.toLowerCase()) {
            case "owner": {
                roleKey = SystemRole.PMS_ORG_ADMIN.getRoleKey();
                roleName = SystemRole.PMS_ORG_ADMIN.getRoleName();
                break;
            }
            case "admin": {
                roleKey = SystemRole.PMS_PROJECT_MANAGER.getRoleKey();
                roleName = SystemRole.PMS_PROJECT_MANAGER.getRoleName();
                break;
            }
            default: {
                roleKey = SystemRole.PMS_USER.getRoleKey();
                roleName = SystemRole.PMS_USER.getRoleName();
                break;
            }
        }
        SimpleSysRole sysRole = new SimpleSysRole();
        sysRole.setRoleName(roleName);
        sysRole.setRoleKey(roleKey);
        target.setRole(sysRole);
        return target;
    }
}
