package cn.axzo.msg.center.service.domain;

import cn.axzo.msg.center.service.domain.url.AppUrl;
import cn.axzo.msg.center.service.domain.url.WebUrl;
import cn.axzo.msg.center.service.enums.WebPageOpenStrategy;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

/**
 * <a href="https://docs.dingtalk.com/i/nodes/14lgGw3P8v9Yk4yaF0aAo9NbJ5daZ90D">REQ-3045</a>
 *
 * @author yanglin
 */
@Setter
@Getter
public class UrlConfig {

    /**
     * PC(OMS)
     */
    private WebUrl pcOms;

    /**
     * PC(CMS)
     */
    private WebUrl pcCms;

    /**
     * 监管平台
     */
    private WebUrl pcGaGeneral;

    /**
     * APP(工人端)
     */
    private MobileUrlConfig appWorker;

    /**
     * APP(管理端)
     */
    private MobileUrlConfig appManager;

    @JsonIgnore @JSONField(serialize = false)
    public WebUrl getOrCreatePcOms() {
        if (pcOms == null)
            pcOms = new WebUrl();
        return pcOms;
    }

    @JsonIgnore @JSONField(serialize = false)
    public WebUrl getOrCreatePcCms() {
        if (pcCms == null)
            pcCms = new WebUrl();
        return pcCms;
    }

    @JsonIgnore @JSONField(serialize = false)
    public WebUrl getOrCreatePcGaGeneral() {
        if (pcGaGeneral == null)
            pcGaGeneral = new WebUrl();
        return pcGaGeneral;
    }

    @JsonIgnore @JSONField(serialize = false)
    public MobileUrlConfig getOrCreateAppWorker() {
        if (appWorker == null)
            appWorker = new MobileUrlConfig();
        return appWorker;
    }

    @JsonIgnore @JSONField(serialize = false)
    public MobileUrlConfig getOrCreateAppManager() {
        if (appManager == null)
            appManager = new MobileUrlConfig();
        return appManager;
    }

    public boolean hasUrl() {
        if (pcOms != null && pcOms.hasUrl())
            return true;
        if (pcCms != null && pcCms.hasUrl())
            return true;
        if (pcGaGeneral != null && pcGaGeneral.hasUrl())
            return true;
        if (appWorker != null && appWorker.hasUrl())
            return true;
        return appManager != null && appManager.hasUrl();
    }

    public void setFieldsNullIfUrlAbsent() {
        if (pcOms != null && !pcOms.hasUrl())
            pcOms = null;
        if (pcCms != null && !pcCms.hasUrl())
            pcCms = null;
        if (pcGaGeneral != null && !pcGaGeneral.hasUrl())
            pcGaGeneral = null;
        if (appWorker != null)
            appWorker.setFieldsNullIfUrlAbsent();
        if (appWorker != null && !appWorker.hasUrl())
            appWorker = null;
        if (appManager != null)
            appManager.setFieldsNullIfUrlAbsent();
        if (appManager != null && !appManager.hasUrl())
            appManager = null;
    }

    public void setDefaultUrl(String url) {
        applyUrlAsDefaults(url);
    }

    public void applyUrlAsDefaults(String url) {
        getOrCreatePcOms().setOpenStrategy(WebPageOpenStrategy.CURRENT_PAGE);
        getOrCreatePcOms().setUrl(url);

        getOrCreatePcCms().setOpenStrategy(WebPageOpenStrategy.CURRENT_PAGE);
        getOrCreatePcCms().setUrl(url);

        getOrCreatePcGaGeneral().setOpenStrategy(WebPageOpenStrategy.CURRENT_PAGE);
        getOrCreatePcGaGeneral().setUrl(url);

        AppUrl appUrl = new AppUrl();
        appUrl.setUrl(url);

        getOrCreateAppManager().setAndroid(appUrl);
        getOrCreateAppManager().setIos(appUrl);
        getOrCreateAppWorker().setAndroid(appUrl);
        getOrCreateAppWorker().setIos(appUrl);
    }

}