package cn.axzo.msg.center.service.pending.response.v3.model;

import cn.axzo.msg.center.service.domain.MobileUrlConfig;
import cn.axzo.msg.center.service.domain.UrlConfig;
import cn.axzo.msg.center.service.domain.parse.SessionUrl;
import cn.axzo.msg.center.service.domain.url.WebUrl;
import cn.axzo.msg.center.service.enums.AppTerminalTypeEnum;
import cn.axzo.msg.center.service.enums.TerminalTypeEnum;
import cn.axzo.msg.center.service.pending.ClientRequest;
import lombok.RequiredArgsConstructor;

import javax.annotation.Nullable;
import java.util.Optional;

import static cn.axzo.msg.center.service.enums.AppTerminalTypeEnum.B_ENTERPRISE_APP;
import static cn.axzo.msg.center.service.enums.AppTerminalTypeEnum.CMS_WEB_PC;
import static cn.axzo.msg.center.service.enums.AppTerminalTypeEnum.C_WORKER_APP;
import static cn.axzo.msg.center.service.enums.AppTerminalTypeEnum.OMS_WEB_PC;
import static cn.axzo.msg.center.service.enums.AppTerminalTypeEnum.PC_GA_GENERAL;

/**
 * @author yanglin
 */
@RequiredArgsConstructor
public class SessionUrlSelector {

    @Nullable private final ClientRequest request;

    /**
     * 根据请求客户端真实的终端类型来选择对应的URL
     */
    public Optional<SessionUrl> selectFrom(UrlConfig config) {
        if (config == null) return Optional.empty();
        // PC(CMS)
        SessionUrl pcCms = selectWeb(CMS_WEB_PC, config.getPcCms());
        if (pcCms != null) return Optional.of(pcCms);
        // PC(OMS)
        SessionUrl pcOms = selectWeb(OMS_WEB_PC, config.getPcOms());
        if (pcOms != null) return Optional.of(pcOms);
        // PC(政务系统)
        SessionUrl pcGaGeneral = selectWeb(PC_GA_GENERAL, config.getPcGaGeneral());
        if (pcGaGeneral != null) return Optional.of(pcGaGeneral);
        // APP工人端
        SessionUrl appWorker = selectApp(C_WORKER_APP, config.getAppWorker());
        if (appWorker != null) return Optional.of(appWorker);
        // APP管理端
        SessionUrl appManager = selectApp(B_ENTERPRISE_APP, config.getAppManager());
        return Optional.ofNullable(appManager);
    }

    private SessionUrl selectWeb(
            AppTerminalTypeEnum selectAppType, WebUrl webUrl) {
        if (request == null)
            return null;
        if (request.getAppTerminalType() != selectAppType)
            return null;
        if (webUrl == null)
            return null;
        return new SessionUrl(TerminalTypeEnum.WEB, webUrl);
    }

    private SessionUrl selectApp(
            AppTerminalTypeEnum selectAppType, MobileUrlConfig mobileUrl) {
        if (request == null)
            return null;
        if (request.getAppTerminalType() != selectAppType)
            return null;
        if (mobileUrl == null)
            return null;
        TerminalTypeEnum terminalType = request.getTerminalType();
        if (mobileUrl.getIos() != null && terminalType == TerminalTypeEnum.IOS)
            return new SessionUrl(TerminalTypeEnum.IOS, mobileUrl.getIos());
        if (mobileUrl.getAndroid() != null && terminalType == TerminalTypeEnum.ANDROID)
            return new SessionUrl(TerminalTypeEnum.ANDROID, mobileUrl.getAndroid());
        return null;
    }

}