package cn.axzo.msg.center.message.service.todo;

import cn.axzo.msg.center.api.custombutton.ProposedButtons;
import cn.axzo.msg.center.common.utils.BizAssertions;
import cn.axzo.msg.center.domain.entity.PendingRecordAdapter;
import cn.axzo.msg.center.domain.entity.PendingRecordExt;
import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.domain.entity.TodoBusiness;
import cn.axzo.msg.center.service.enums.BizCategoryEnum;
import cn.axzo.msg.center.service.enums.BizFinalStateEnum;
import cn.axzo.msg.center.service.enums.IdentityTypeEnum;
import cn.axzo.msg.center.service.enums.OrganizationTypeEnum;
import cn.axzo.msg.center.service.enums.PendingMessageStateEnum;
import cn.axzo.msg.center.service.enums.TodoType;
import cn.axzo.msg.center.service.enums.YesOrNo;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

import java.util.Date;

/**
 * @author yanglin
 */
@Getter
public class TodoRecordAdapter implements PendingRecordAdapter {

    private final TodoBusiness business;
    private final Todo todo;
    private final boolean forBusiness;

    private TodoRecordAdapter(TodoBusiness business, Todo todo, boolean forBusiness) {
        this.business = business;
        this.todo = todo;
        this.forBusiness = forBusiness;
    }

    public static TodoRecordAdapter adaptBusiness(TodoBusiness business, Todo todo) {
        BizAssertions.assertNotNull(business, "待办业务为空");
        BizAssertions.assertNotNull(todo, "待办为空");
        return new TodoRecordAdapter(business, todo, true);
    }

    public static TodoRecordAdapter adaptTodo(TodoBusiness business, Todo todo) {
        BizAssertions.assertNotNull(business, "待办业务为空");
        BizAssertions.assertNotNull(todo, "待办为空");
        return new TodoRecordAdapter(business, todo, false);
    }

    @Override
    public String getIdentityCode() {
        return todo.getIdentityCode();
    }

    @Override
    public String getRequestNo() {
        return todo.getRequestNo();
    }

    @Override
    public Long getPromoterId() {
        return business.getPromoterId();
    }

    @Override
    public Long getPromoterPersonId() {
        return business.getPromoterPersonId();
    }

    @Override
    public String getPromoterName() {
        return business.getPromoterName();
    }

    @Override
    public IdentityTypeEnum getPromoterType() {
        return business.getPromoterType();
    }

    @Override
    public Long getExecutorId() {
        return todo.getExecutorId();
    }

    @Override
    public Long getExecutorPersonId() {
        return todo.getExecutorPersonId();
    }

    @Override
    public String getExecutorName() {
        return todo.getExecutorName();
    }

    @Override
    public IdentityTypeEnum getExecutorType() {
        return todo.getExecutorType();
    }

    @Override
    public String getTemplateCode() {
        return todo.getTemplateCode();
    }

    @Override
    public String getTitle() {
        return todo.getTitle();
    }

    @Override
    public String getContent() {
        return todo.getContent();
    }

    @Override
    public OrganizationTypeEnum getOrgType() {
        return todo.getOrgType();
    }

    @Override
    public Long getPromoterOrgId() {
        return business.getOrgId();
    }

    @Override
    public OrganizationTypeEnum getPromoterOrgType() {
        return business.getOrgType();
    }

    @Override
    public OrganizationTypeEnum getExecutorOrgType() {
        return todo.getOrgType();
    }

    @Override
    public String getPromoterOrgName() {
        return business.getOrgName();
    }

    @Override
    public Long getPromoterOuId() {
        return business.getOuId();
    }

    @Override
    public Long getOrgId() {
        return todo.getOrgId();
    }

    @Override
    public String getOrgName() {
        return todo.getOrgName();
    }

    @Override
    public Long getOuId() {
        return todo.getOuId();
    }

    @Override
    public PendingMessageStateEnum getState() {
        return todo.getState();
    }

    @Override
    public BizCategoryEnum getBizCategory() {
        return business.getBizCategory();
    }

    @Override
    public String getBizCode() {
        return todo.getBizCode();
    }

    @Override
    public String getSubBizCode() {
        return forBusiness ? "" : todo.getSubBizCode();
    }

    @Override
    public String getBizDesc() {
        return todo.getBizDesc();
    }

    @Override
    public String getBizFlag() {
        return business.getBizFlag();
    }

    @Override
    public String getBizExtParam() {
        JSONObject obj = todo.getBizExtParam();
        return obj == null ? null : obj.toJSONString();
    }

    @Override
    public String getRouterParams() {
        JSONObject obj = todo.getRouterParams();
        return obj == null ? null : obj.toJSONString();
    }

    @Override
    public BizFinalStateEnum getBizFinalState() {
        return business.getBizFinalState();
    }

    @Override
    public Date getDeadline() {
        return business.getDeadline();
    }

    @Override
    public Integer getRetryCount() {
        return null;
    }

    @Override
    public Date getHideUntil() {
        return todo.getHideUntil();
    }

    @Override
    public PendingRecordExt getRecordExt() {
        return todo.getRecordExt();
    }

    @Override
    public YesOrNo getIsOuIdMigrated() {
        return todo.getIsOuIdMigrated();
    }

    @Override
    public Date getCreateAt() {
        return todo.getCreateAt();
    }

    @Override
    public Date getUpdateAt() {
        return todo.getUpdateAt();
    }

    @Override
    public TodoType getTodoType() {
        return todo.getType();
    }

    @Override
    public Boolean isRead() {
        return getState() == PendingMessageStateEnum.READ;
    }

    @Override
    public TodoBusiness getTodoBusiness() {
        return business;
    }

    @Override
    public ProposedButtons getCustomButtons() {
        return forBusiness ? business.getProposedButtons() : todo.getProposedButtons();
    }
}
