package com.njzscloud.dispose.sys.auth.pojo.result;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.njzscloud.common.security.support.UserDetail;
import com.njzscloud.dispose.cst.customer.constant.IdentityCategory;
import com.njzscloud.dispose.sys.user.constant.Gender;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.njzscloud.common.security.contant.Constants.ROLE_ADMIN;

@Getter
@Setter
@Accessors(chain = true)
public class MyResult extends UserDetail {

    public static final String ROLE_KANLIAO = "ROLE_KANLIAO";
    public static final String ROLE_XiaoNa = "ROLE_XiaoNa";
    public static final String ROLE_ChanFei = "ROLE_ChanFei";
    public static final String ROLE_YunShu = "ROLE_YunShu";
    public static final String ROLE_CaiGou = "ROLE_CaiGou";
    public static final String ROLE_SiJi = "ROLE_SiJi";

    /**
     * 菜单信息
     */
    private List<MenuResource> menus;

    /**
     * 端点信息
     */
    private List<EndpointResource> endpoints;
    /**
     * 身份信息
     */
    private List<IdentityInfo> identities;

    /**
     * 系统设置
     */
    private List<Map<String, Object>> setting;

    /**
     * 当前身份 Id
     *
     */
    private Long currentCustomerId;

    /**
     * 当前组织 Id
     *
     */
    private Long currentOrgId;

    /**
     * 头像
     */
    private String avatar;
    /**
     * 性别; 字典代码：gender
     */
    private Gender gender;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 余额
     */
    private BigDecimal recharge;
    /**
     * 营收
     */
    private BigDecimal revenue;

    @JsonProperty("currentIdentity")
    public IdentityInfo currentIdentity() {
        return identities.stream()
            .filter(it -> Objects.equals(it.getCustomerId(), currentCustomerId))
            .findFirst()
            .orElseGet(IdentityInfo::new);
    }

    /**
     * 是否是看料员
     *
     * @return true/false
     */
    @JsonProperty("isKanLiao")
    public boolean isKanLiao() {
        return this.getRoles().contains(ROLE_KANLIAO);
    }

    @JsonProperty("isXiaoNa")
    public boolean isXiaoNa() {
        return this.currentIdentity().getIdentityCategory() == IdentityCategory.XiaoNa;
    }

    @JsonProperty("isChanFei")
    public boolean isChanFei() {
        return this.currentIdentity().getIdentityCategory() == IdentityCategory.ChanFei;
    }

    @JsonProperty("isCaiGou")
    public boolean isCaiGou() {
        return this.currentIdentity().getIdentityCategory() == IdentityCategory.CaiGou;
    }

    @JsonProperty("isYunShu")
    public boolean isYunShu() {
        return this.currentIdentity().getIdentityCategory() == IdentityCategory.YunShu;
    }

    @JsonProperty("isSiJi")
    public boolean isSiJi() {
        return this.currentIdentity().getIdentityCategory() == IdentityCategory.SiJi;
    }

    @JsonProperty("isAdmin")
    public boolean isAdmin() {
        return this.getRoles().contains(ROLE_ADMIN);

    }

    @JsonProperty("hasXiaoNa")
    public boolean hasXiaoNa() {
        return this.getRoles().contains(ROLE_XiaoNa);
    }

    @JsonProperty("hasChanFei")
    public boolean hasChanFei() {
        return this.getRoles().contains(ROLE_ChanFei);
    }

    @JsonProperty("hasCaiGou")
    public boolean hasCaiGou() {
        return this.getRoles().contains(ROLE_CaiGou);
    }

    @JsonProperty("hasYunShu")
    public boolean hasYunShu() {
        return this.getRoles().contains(ROLE_YunShu);
    }

    @JsonProperty("hasSiJi")
    public boolean hasSiJi() {
        return this.getRoles().contains(ROLE_SiJi);
    }
}
