<template>
    <section v-if="networkBoolean" :class="['network-information', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']" id="network">Network specifications:</div>
        <div class="asset-data">
            <div class="IPv4-MAC">
                <div class="data-field" id="IPv4">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">IPv4:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ inputIPv4 }}</pre>
                    <input v-if="editable" v-model="inputIPv4" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
                <div class="data-field" id="MAC">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">MAC:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ item.MAC }}</pre>
                    <input v-if="editable" v-model="item.MAC" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
            <div class="IPv6-subnetmask">
                <div class="data-field" id="IPv6">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">IPv6:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ inputIPv6 }}</pre>
                    <input v-if="editable" v-model="inputIPv6" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
                <div class="data-field" id="subnetmask">
                    <pre :class="['label', darkMode ? 'div-darkmode' : 'div-lightmode']">Subnetmask:</pre>
                    <pre v-if="!editable"
                        :class="['data', darkMode ? 'data-darkmode' : 'data-lightmode']">{{ item.subnetmask }}</pre>
                    <input v-if="editable" v-model="item.subnetmask" @change="updateConfigItem()"
                        :class="['data', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
                </div>
            </div>
        </div>
    </section>
</template>
    

<script setup>
import { ref, onMounted } from 'vue';
import Axios from '../axios.config.js';
import clientsideConfig from '../../clientsideConfig.js';
import { useStore } from 'vuex';
import { computed } from 'vue';

const store = useStore();
const editable = computed(() => store.state.assetEditable);
const chosenAssetId = computed(() => store.state.chosenAssetId);

const darkMode = ref(true)
const item = ref({});
const networkBoolean = ref(false);
const inputIPv4 = ref('');
const inputIPv6 = ref('');

// get config item from id
const getItemById = async () => {
    try {
        const response = await Axios.get(
            `https://${clientsideConfig.url}:${clientsideConfig.port}/api/getConfigItem/${chosenAssetId.value}`
        );
        item.value = response.data;
        if ((item.value.networkBool == 1) && (item.value.IPv4 === (null | "")) && (item.value.IPv6 === (null | "")) && (item.value.subnetmask === (null | "")) && (item.value.MAC === (null | ""))) {
            item.value.networkBool = 0;
            networkBoolean.value = false;
        };
        if (item.value.networkBool == 1) {
            networkBoolean.value = true;
        };

        if (item.value.IPv4 == null) {
            inputIPv4.value = "";
        } else {
            inputIPv4.value = item.value.IPv4;
        }
        if (item.value.IPv6 == null) {
            inputIPv6.value = "";
        } else {
            inputIPv6.value = item.value.IPv6;
        }
    } catch (err) {
        console.log(err.response.statusText);
    }
}

// helper function to validate an IPv4 address
const validateIPv4address = (ipaddress) => {
    const ipv4format = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    if (ipv4format.test(ipaddress) || ipaddress === "") {
        return true;
    } else {
        return false;
    }
}

// helper function to validate an IPv4 address
const validateIPv6address = (ipaddress) => {
    const ipv6format = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/gi;
    if (ipv6format.test(ipaddress) || ipaddress === "") {
        return true;
    } else {
        return false;
    }
}

//update data
const updateConfigItem = async () => {
    if (validateIPv4address(inputIPv4.value)) {
        item.value.IPv4 = inputIPv4.value;
        try {
            await Axios.put(
                `https://${clientsideConfig.url}:${clientsideConfig.port}/api/updateConfigItem`,
                {
                    primaryID: item.value.primaryID,
                    assetName: item.value.assetName,
                    customerID: item.value.customerID,
                    customer: item.value.customer,
                    location: item.value.location,
                    remoteLocation: item.value.remoteLocation,
                    type: item.value.type,
                    description: item.value.description,
                    notes: item.value.notes,
                    state: item.value.state,
                    lastView: item.value.lastView,
                    user: item.value.user,
                    hardwareBool: item.value.hardwareBool,
                    model: item.value.model,
                    serialnumber: item.value.serialnumber,
                    CPU: item.value.CPU,
                    RAM: item.value.RAM,
                    storageConfiguration: item.value.storageConfiguration,
                    miscellaneous: item.value.miscellaneous,
                    softwareBool: item.value.softwareBool,
                    software: item.value.software,
                    version: item.value.version,
                    license: item.value.license,
                    networkBool: item.value.networkBool,
                    IPv4: item.value.IPv4,
                    IPv6: item.value.IPv6,
                    MAC: item.value.MAC,
                    subnetmask: item.value.subnetmask,
                }
            );
        } catch (err) {
            console.log(err.response.statusText);
        }
    } else {
        if (item.value.IPv4 == null) {
            inputIPv4.value = "";
        } else {
            inputIPv4.value = item.value.IPv4;
        }
        alert("You have entered an invalid IPv4 address, please choose a valid one!");
    }
    if (validateIPv6address(inputIPv6.value)) {
        item.value.IPv6 = inputIPv6.value;
        try {
            await Axios.put(
                `https://${clientsideConfig.url}:${clientsideConfig.port}/api/updateConfigItem`,
                {
                    primaryID: item.value.primaryID,
                    assetName: item.value.assetName,
                    customerID: item.value.customerID,
                    customer: item.value.customer,
                    location: item.value.location,
                    remoteLocation: item.value.remoteLocation,
                    type: item.value.type,
                    description: item.value.description,
                    notes: item.value.notes,
                    state: item.value.state,
                    lastView: item.value.lastView,
                    user: item.value.user,
                    hardwareBool: item.value.hardwareBool,
                    model: item.value.model,
                    serialnumber: item.value.serialnumber,
                    CPU: item.value.CPU,
                    RAM: item.value.RAM,
                    storageConfiguration: item.value.storageConfiguration,
                    miscellaneous: item.value.miscellaneous,
                    softwareBool: item.value.softwareBool,
                    software: item.value.software,
                    version: item.value.version,
                    license: item.value.license,
                    networkBool: item.value.networkBool,
                    IPv4: item.value.IPv4,
                    IPv6: item.value.IPv6,
                    MAC: item.value.MAC,
                    subnetmask: item.value.subnetmask,
                }
            );
        } catch (err) {
            console.log(err.response.statusText);
        }
    } else {
        if (item.value.IPv6 == null) {
            inputIPv6.value = "";
        } else {
            inputIPv6.value = item.value.IPv6;
        }
        alert("You have entered an invalid IPv6 address, please choose a valid one!");
    }
}

onMounted(() => {
    getItemById();
});
</script>

<script>
export default {
    name: "NetworkSpecifications",
};
</script>          
      
      
<style scoped>
.network-information {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    width: 100%;
    padding: 1.25rem 1.875rem;
    border-radius: 0.625rem;
    box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

.section-darkmode {
    background-color: #2c2c2c;
}

.section-lightmode {
    background-color: #fff;
}

.data-field {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    padding: 0 1.875rem;
    gap: 1.25rem;
    border-radius: 0.625rem;
}

.label {
    letter-spacing: 5%;
    font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

.div-darkmode {
    color: #fff;
}

.div-lightmode {
    color: #000;
}

.label#network {
    padding-top: 1rem;
    padding-bottom: 1rem;
}

.data {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    padding: 0 0.625rem;
    border-radius: 0.3125rem;
    box-shadow: 0.0625rem 0.0625rem 0.25rem 0rem rgba(0, 0, 0, 0.25) inset;
    letter-spacing: 5%;
    font: 400 0.75rem/250% Overpass, sans-serif;
}

.data-darkmode {
    background-color: #212121;
    color: #fff;
}

.data-lightmode {
    background-color: #EBEBEB;
    color: #000;
}

.asset-data {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    align-self: stretch;
}

.IPv4-MAC,
.IPv6-subnetmask {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    line-height: normal;
    width: 50%;
    padding: 0 0;
    border-radius: 0.3125rem;
}

.input {
    border: none;
}
</style>