package top.octopusyan.model;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import org.apache.commons.lang3.StringUtils;
import top.octopusyan.config.ProxyConfig;
import top.octopusyan.manager.http.request.ProxySetup;
import top.octopusyan.utils.DomainUtil;

/**
 * @author : octopus yan
 * @email : octopus_yan@foxmail.com
 * @description :  用户隧道设置模型
 * @create : 2022-4-6 16:45
 */
public class ProxySetupModel {
    private final SimpleStringProperty id = new SimpleStringProperty();
    private final SimpleStringProperty server = new SimpleStringProperty();
    private final SimpleStringProperty proxyName = new SimpleStringProperty();
    private final SimpleStringProperty proxyType = new SimpleStringProperty();
    private final SimpleStringProperty localIp = new SimpleStringProperty();
    private final SimpleStringProperty localPort = new SimpleStringProperty();
    private final SimpleStringProperty remotePort = new SimpleStringProperty();
    private final SimpleStringProperty domain = new SimpleStringProperty();
    private final SimpleStringProperty domainSuffix = new SimpleStringProperty();
    private final SimpleBooleanProperty useEncryption = new SimpleBooleanProperty();
    private final SimpleBooleanProperty useCompression = new SimpleBooleanProperty();
    private final SimpleStringProperty locations = new SimpleStringProperty();
    private final SimpleStringProperty hostHeaderRewrite = new SimpleStringProperty();
    private final SimpleStringProperty header_X_From_Where = new SimpleStringProperty();
    private final SimpleStringProperty sk = new SimpleStringProperty();
    private final SimpleBooleanProperty status = new SimpleBooleanProperty();
    private final SimpleStringProperty serverName = new SimpleStringProperty();
    private final SimpleIntegerProperty sort = new SimpleIntegerProperty();
    private final SimpleBooleanProperty running = new SimpleBooleanProperty();
    private final SimpleBooleanProperty isCustomize = new SimpleBooleanProperty();
    private final SimpleStringProperty httpUser = new SimpleStringProperty();
    private final SimpleStringProperty httpPwd = new SimpleStringProperty();
    public volatile Boolean isProvider = null;

    public ProxySetupModel(ProxySetup setup) {
        if (setup == null) return;

        String domainStr = setup.getDomain();
        String suffix = "";
        isCustomize.set(DomainUtil.isCustomize(setup));
        if (!isCustomize.get()) {
            domainStr = DomainUtil.getCustomize(setup);
            suffix = DomainUtil.getSuffix(setup);
        }
        setId(setup.getId());
        setServer(ProxyConfig.getServerName(setup.getNode()));
        setProxyName(setup.getProxy_name());
        setProxyType(StringUtils.upperCase(setup.getProxy_type()));
        setLocalIp(setup.getLocal_ip());
        setLocalPort(setup.getLocal_port());
        setRemotePort(setup.getRemote_port());
        setDomain(domainStr);
        setHttpUser(setup.getHttp_user());
        setHttpPwd(setup.getHttp_pwd());
        setDomainSuffix(suffix);
        setLocations(setup.getLocations());
        setUseEncryption(setup.getUse_encryption());
        setUseCompression(setup.getUse_compression());
        setHeader_X_From_Where(setup.getHost_header_rewrite());
        setHostHeaderRewrite(setup.getHost_header_rewrite());
        setStatus(setup.getStatus() == null || setup.getStatus());
        setServerName(setup.getServer_name());
        setSk(setup.getSk());
        setSort(setup.getSort());
        setRunning(setup.isRuning());
    }

    public String getId() {
        return id.get();
    }

    public String getServer() {
        return server.get();
    }

    public String getProxyName() {
        return proxyName.get();
    }

    public String getProxyType() {
        return StringUtils.lowerCase(proxyType.get());
    }

    public String getLocalPort() {
        return localPort.get();
    }

    public String getRemotePort() {
        return remotePort.get();
    }

    public Integer getSort() {
        return sort.get();
    }

    public String getLocalIp() {
        return localIp.get();
    }

    public String getDomain() {
        return domain.get();
    }

    public String getDomainSuffix() {
        return domainSuffix.get();
    }

    public String getHeader_X_From_Where() {
        return header_X_From_Where.get();
    }

    public String getHostHeaderRewrite() {
        return hostHeaderRewrite.get();
    }

    public String getLocations() {
        return locations.get();
    }

    public String getSk() {
        return sk.get();
    }

    public boolean isRunning() {
        return running.get();
    }

    public boolean getStatus() {
        return status.get();
    }

    public Boolean isProvider() {
        return serverName.get() == null ? null : serverName.get().equals("提供者");
    }

    public String getServerName() {
        return serverName.get();
    }

    public boolean isUseCompression() {
        return useCompression.get();
    }

    public boolean isUseEncryption() {
        return useEncryption.get();
    }

    public String getHttpUser() {
        return httpUser.get();
    }

    public String getHttpPwd() {
        return httpPwd.get();
    }

    public void setId(Integer id) {
        this.id.set(id == null ? null : id.toString());
    }

    public void setProxyName(String proxyName) {
        this.proxyName.set(proxyName);
    }

    public void setProxyType(String proxyType) {
        this.proxyType.set(StringUtils.upperCase(proxyType));
    }

    public void setServer(String server) {
        this.server.set(server);
    }

    public void setLocalIp(String localIp) {
        this.localIp.set(localIp);
    }

    public void setDomain(String domain) {
        this.domain.set(domain);
    }

    public void setDomainSuffix(String domainSuffix) {
        this.domainSuffix.set(domainSuffix);
    }

    public void setHeader_X_From_Where(String header_X_From_Where) {
        this.header_X_From_Where.set(header_X_From_Where);
    }

    public void setHostHeaderRewrite(String hostHeaderRewrite) {
        this.hostHeaderRewrite.set(hostHeaderRewrite);
    }

    public void setLocalPort(Integer localPort) {
        this.localPort.set(Integer.toString(localPort));
    }

    public void setRemotePort(String remotePort) {
        this.remotePort.set(remotePort);
    }

    public void setLocations(String locations) {
        this.locations.set(locations);
    }

    public void setRunning(Boolean running) {
        this.running.set(running);
    }

    public void setUseCompression(Boolean useCompression) {
        this.useCompression.set(useCompression);
    }

    public void setSk(String sk) {
        this.sk.set(sk);
    }

    public void setSort(Integer sort) {
        if (sort != null) this.sort.set(sort);
    }

    public void setStatus(boolean status) {
        this.status.set(status);
    }

    public void setServerName(String serverName) {
        this.serverName.set(serverName);
    }

    public void setUseEncryption(boolean useEncryption) {
        this.useEncryption.set(useEncryption);
    }

    public void setHttpUser(String httpUser) {
        this.httpUser.set(httpUser);
    }

    public void setHttpPwd(String httpPwd) {
        this.httpPwd.set(httpPwd);
    }

    public SimpleStringProperty httpPwdProperty() {
        return httpPwd;
    }

    public SimpleStringProperty httpUserProperty() {
        return httpUser;
    }

    public SimpleStringProperty serverProperty() {
        return server;
    }

    public SimpleStringProperty proxyNameProperty() {
        return proxyName;
    }

    public SimpleStringProperty proxyTypeProperty() {
        return proxyType;
    }

    public SimpleBooleanProperty runningProperty() {
        return running;
    }

    public SimpleBooleanProperty statusProperty() {
        return status;
    }

    public SimpleBooleanProperty useCompressionProperty() {
        return useCompression;
    }

    public SimpleBooleanProperty useEncryptionProperty() {
        return useEncryption;
    }

    public SimpleStringProperty idProperty() {
        return id;
    }

    public SimpleStringProperty localPortProperty() {
        return localPort;
    }

    public SimpleStringProperty remotePortProperty() {
        return remotePort;
    }

    public SimpleIntegerProperty sortProperty() {
        return sort;
    }

    public SimpleStringProperty domainProperty() {
        return domain;
    }

    public SimpleStringProperty domainSuffixProperty() {
        return domainSuffix;
    }

    public SimpleStringProperty header_X_From_WhereProperty() {
        return header_X_From_Where;
    }

    public SimpleStringProperty localIpProperty() {
        return localIp;
    }

    public SimpleStringProperty locationsProperty() {
        return locations;
    }

    public SimpleStringProperty skProperty() {
        return sk;
    }

    public SimpleStringProperty hostHeaderRewriteProperty() {
        return hostHeaderRewrite;
    }

    public SimpleStringProperty serverNameProperty() {
        return serverName;
    }

    public SimpleBooleanProperty isCustomizeProperty() {
        return isCustomize;
    }

    public void set(ProxySetup setup) {
        String domainStr = setup.getDomain();
        String suffix = "";
        isCustomize.set(DomainUtil.isCustomize(setup));
        if (!isCustomize.get()) {
            domainStr = DomainUtil.getCustomize(setup);
            suffix = DomainUtil.getSuffix(setup);
        }
        setId(setup.getId());
        setProxyName(setup.getProxy_name());
        setProxyType(setup.getProxy_type());
        setLocalIp(setup.getLocal_ip());
        setLocalPort(setup.getLocal_port());
        setRemotePort(setup.getRemote_port());
        setDomain(domainStr);
        setDomainSuffix(suffix);
        setLocations(setup.getLocations());
        setUseEncryption(setup.getUse_encryption());
        setUseCompression(setup.getUse_compression());
        setHeader_X_From_Where(setup.getHost_header_rewrite());
        setHostHeaderRewrite(setup.getHost_header_rewrite());
        setStatus(setup.getStatus() == null || setup.getStatus());
        setServerName(setup.getServer_name());
        setSk(setup.getSk());
        setSort(setup.getSort());
        setRunning(setup.isRuning());
        // 数据绑定原因 放在最后
        setServer(ProxyConfig.getServerName(setup.getNode()));
    }

    public ProxySetup get() {
        return new ProxySetup(
                getId() == null ? null : Integer.parseInt(getId()),
                ProxyConfig.getServerNode(server.get()),
                getProxyName(),
                StringUtils.lowerCase(proxyType.getValue()),
                getLocalIp(),
                getLocalPort() == null ? null : Integer.parseInt(getLocalPort()),
                getRemotePort(),
                getDomain() == null ? null : getDomain() + getDomainSuffix(),
                ProxyConfig.isHttp(this) ? getHttpUser() : null,
                ProxyConfig.isHttp(this) ? getHttpPwd() : null,
                isUseEncryption(),
                isUseCompression(),
                getLocations(),
                getHostHeaderRewrite(),
                getHeader_X_From_Where(),
                getSk(),
                getSort(),
                serverName.get(),
                getStatus(),
                isRunning()
        );
    }
}
