package com.ai.trackwalker.uavaccess.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "uav-access")
public class UavAccessProperties {
    private String internalToken = "local-uav-access-token";
    private String eventCallbackToken = "local-flighthub-event-token";
    private String eventTopic = "uav.access.events.v1";
    private long outboxRelayDelayMs = 5000;
    private FlightHub flighthub = new FlightHub();
    private Mqtt mqtt = new Mqtt();

    public String getInternalToken() {
        return internalToken;
    }

    public void setInternalToken(String internalToken) {
        this.internalToken = internalToken;
    }

    public String getEventTopic() {
        return eventTopic;
    }

    public String getEventCallbackToken() {
        return eventCallbackToken;
    }

    public void setEventCallbackToken(String eventCallbackToken) {
        this.eventCallbackToken = eventCallbackToken;
    }

    public void setEventTopic(String eventTopic) {
        this.eventTopic = eventTopic;
    }

    public long getOutboxRelayDelayMs() {
        return outboxRelayDelayMs;
    }

    public void setOutboxRelayDelayMs(long outboxRelayDelayMs) {
        this.outboxRelayDelayMs = outboxRelayDelayMs;
    }

    public FlightHub getFlighthub() {
        return flighthub;
    }

    public void setFlighthub(FlightHub flighthub) {
        this.flighthub = flighthub;
    }

    public Mqtt getMqtt() {
        return mqtt;
    }

    public void setMqtt(Mqtt mqtt) {
        this.mqtt = mqtt;
    }

    public static class FlightHub {
        private boolean enabled;
        private String baseUrl = "";
        private String userToken = "";
        private String userTokenFile = "";
        private String organizationUuid = "";
        private String projectUuid = "";
        private int connectTimeoutSeconds = 5;
        private int readTimeoutSeconds = 30;
        private boolean allowInsecureHttp;
        private boolean flightControlEnabled;
        private boolean storagePathStyleAccess = true;
        private Wpml wpml = new Wpml();
        private Mission mission = new Mission();

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getBaseUrl() {
            return baseUrl;
        }

        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }

        public String getUserToken() {
            return userToken;
        }

        public void setUserToken(String userToken) {
            this.userToken = userToken;
        }

        public String getUserTokenFile() {
            return userTokenFile;
        }

        public void setUserTokenFile(String userTokenFile) {
            this.userTokenFile = userTokenFile;
        }

        public String getOrganizationUuid() {
            return organizationUuid;
        }

        public void setOrganizationUuid(String organizationUuid) {
            this.organizationUuid = organizationUuid;
        }

        public String getProjectUuid() {
            return projectUuid;
        }

        public void setProjectUuid(String projectUuid) {
            this.projectUuid = projectUuid;
        }

        public int getConnectTimeoutSeconds() {
            return connectTimeoutSeconds;
        }

        public void setConnectTimeoutSeconds(int connectTimeoutSeconds) {
            this.connectTimeoutSeconds = connectTimeoutSeconds;
        }

        public int getReadTimeoutSeconds() {
            return readTimeoutSeconds;
        }

        public void setReadTimeoutSeconds(int readTimeoutSeconds) {
            this.readTimeoutSeconds = readTimeoutSeconds;
        }

        public boolean isAllowInsecureHttp() {
            return allowInsecureHttp;
        }

        public void setAllowInsecureHttp(boolean allowInsecureHttp) {
            this.allowInsecureHttp = allowInsecureHttp;
        }

        public boolean isFlightControlEnabled() {
            return flightControlEnabled;
        }

        public void setFlightControlEnabled(boolean flightControlEnabled) {
            this.flightControlEnabled = flightControlEnabled;
        }

        public boolean isStoragePathStyleAccess() {
            return storagePathStyleAccess;
        }

        public void setStoragePathStyleAccess(boolean storagePathStyleAccess) {
            this.storagePathStyleAccess = storagePathStyleAccess;
        }

        public Wpml getWpml() {
            return wpml;
        }

        public void setWpml(Wpml wpml) {
            this.wpml = wpml;
        }

        public Mission getMission() {
            return mission;
        }

        public void setMission(Mission mission) {
            this.mission = mission;
        }

        public List<String> missingRequiredConfiguration(boolean requireWpml) {
            List<String> missing = new ArrayList<>();
            if (blank(baseUrl)) missing.add("FLIGHTHUB_BASE_URL");
            if (blank(projectUuid)) missing.add("FLIGHTHUB_PROJECT_UUID");
            if (blank(organizationUuid)) missing.add("FLIGHTHUB_ORGANIZATION_UUID");
            if (blank(userToken) && blank(userTokenFile)) {
                missing.add("FLIGHTHUB_USER_TOKEN or FLIGHTHUB_USER_TOKEN_FILE");
            }
            if (requireWpml) {
                if (blank(wpml.droneEnumValue)) missing.add("FLIGHTHUB_WPML_DRONE_ENUM_VALUE");
                if (blank(wpml.droneSubEnumValue)) missing.add("FLIGHTHUB_WPML_DRONE_SUB_ENUM_VALUE");
                if (blank(wpml.payloadEnumValue)) missing.add("FLIGHTHUB_WPML_PAYLOAD_ENUM_VALUE");
                if (blank(wpml.payloadSubEnumValue)) missing.add("FLIGHTHUB_WPML_PAYLOAD_SUB_ENUM_VALUE");
            }
            return missing;
        }

        private static boolean blank(String value) {
            return value == null || value.isBlank();
        }
    }

    public static class Wpml {
        private String version = "1.0.6";
        private String author = "AItrackwalker";
        private String droneEnumValue = "";
        private String droneSubEnumValue = "";
        private String payloadEnumValue = "";
        private String payloadSubEnumValue = "";

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public String getDroneEnumValue() {
            return droneEnumValue;
        }

        public void setDroneEnumValue(String droneEnumValue) {
            this.droneEnumValue = droneEnumValue;
        }

        public String getDroneSubEnumValue() {
            return droneSubEnumValue;
        }

        public void setDroneSubEnumValue(String droneSubEnumValue) {
            this.droneSubEnumValue = droneSubEnumValue;
        }

        public String getPayloadEnumValue() {
            return payloadEnumValue;
        }

        public void setPayloadEnumValue(String payloadEnumValue) {
            this.payloadEnumValue = payloadEnumValue;
        }

        public String getPayloadSubEnumValue() {
            return payloadSubEnumValue;
        }

        public void setPayloadSubEnumValue(String payloadSubEnumValue) {
            this.payloadSubEnumValue = payloadSubEnumValue;
        }
    }

    public static class Mission {
        private String timeZone = "Asia/Shanghai";
        private int minimumBatteryPercent = 30;
        private int resumableStatus = 1;
        private int outOfControlAction;
        private int rthAltitudeM = 100;
        private String deviceType = "";

        public String getTimeZone() {
            return timeZone;
        }

        public void setTimeZone(String timeZone) {
            this.timeZone = timeZone;
        }

        public int getMinimumBatteryPercent() {
            return minimumBatteryPercent;
        }

        public void setMinimumBatteryPercent(int minimumBatteryPercent) {
            this.minimumBatteryPercent = minimumBatteryPercent;
        }

        public int getResumableStatus() {
            return resumableStatus;
        }

        public void setResumableStatus(int resumableStatus) {
            this.resumableStatus = resumableStatus;
        }

        public int getOutOfControlAction() {
            return outOfControlAction;
        }

        public void setOutOfControlAction(int outOfControlAction) {
            this.outOfControlAction = outOfControlAction;
        }

        public int getRthAltitudeM() {
            return rthAltitudeM;
        }

        public void setRthAltitudeM(int rthAltitudeM) {
            this.rthAltitudeM = rthAltitudeM;
        }

        public String getDeviceType() {
            return deviceType;
        }

        public void setDeviceType(String deviceType) {
            this.deviceType = deviceType;
        }
    }

    public static class Mqtt {
        private boolean enabled;
        private String brokerUrl = "tcp://localhost:1883";
        private String clientId = "aitrackwalker-uav-access";
        private String username = "";
        private String password = "";
        private String topicFilter = "flighthub2/#";
        private int qos = 1;
        private boolean automaticReconnect = true;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getBrokerUrl() {
            return brokerUrl;
        }

        public void setBrokerUrl(String brokerUrl) {
            this.brokerUrl = brokerUrl;
        }

        public String getClientId() {
            return clientId;
        }

        public void setClientId(String clientId) {
            this.clientId = clientId;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getTopicFilter() {
            return topicFilter;
        }

        public void setTopicFilter(String topicFilter) {
            this.topicFilter = topicFilter;
        }

        public int getQos() {
            return qos;
        }

        public void setQos(int qos) {
            this.qos = qos;
        }

        public boolean isAutomaticReconnect() {
            return automaticReconnect;
        }

        public void setAutomaticReconnect(boolean automaticReconnect) {
            this.automaticReconnect = automaticReconnect;
        }
    }
}
