<template>
    <section v-if="searchable" :class="['solution-search', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['solutionLabel', darkMode ? 'label-darkmode' : 'label-lightmode']">Solution</div>
        <input v-model="solutionSearchFilter" @change="searchSolution()"
            :class="['dataInput', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
    </section>
    <section v-if="filtered" :class="['solution-search', darkMode ? 'section-darkmode' : 'section-lightmode']">
        <div :class="['solutionLabel', darkMode ? 'label-darkmode' : 'label-lightmode']">Type</div>
        <input v-model="typeSearchFilter" @change="filterSolutionByType()"
            :class="['dataInput', 'input', darkMode ? 'data-darkmode' : 'data-lightmode']">
    </section>
    <div :class="['data', darkMode ? 'div-darkmode' : 'div-lightmode']">
        <div :class="['label', darkMode ? 'label-darkmode' : 'label-lightmode']">Solutions:</div>
        <table class="data-table" id="solution-checkklist">
            <tbody>
                <tr :class="['table-row', darkMode ? 'tr-head-darkmode' : 'tr-head-lightmode']" id="table-head">
                    <th
                        :class="['Solution', darkMode ? 'th-darkmode' : 'th-lightmode', darkMode ? 'Solution-darkmode' : 'Solution-lightmode']">
                        Solution</th>
                    <th :class="['Type', darkMode ? 'th-darkmode' : 'th-lightmode']">Type</th>
                </tr>
                <tr v-for="sol in solutions" :key="sol.primaryID"
                    :class="['table-row', darkMode ? 'tr-darkmode' : 'tr-lightmode']" id="row-1">
                    <td
                        :class="['Solution', darkMode ? 'td-darkmode' : 'td-lightmode', darkMode ? 'Solution-darkmode' : 'Solution-lightmode']">
                        <nuxt-link to="/solutions" id="nuxt-link" class="button"
                            :class="[darkMode ? 'button-darkmode' : 'button-lightmode', darkMode ? 'nuxt-link-darkmode' : 'nuxt-link-lightmode']"
                            @click="goToChosenSolution(sol.primaryID)">
                            {{ sol.solutionName }}
                        </nuxt-link>
                    </td>
                    <td :class="['Type', darkMode ? 'td-darkmode' : 'td-lightmode']">{{ sol.type }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</template>


<script setup>
import { ref, onMounted, watch } 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 modeChanged = computed(() => store.state.updateDarkMode);

const chosenAssetId = computed(() => store.state.chosenAssetId);
const searchable = computed(() => store.state.searchable);
const filtered = computed(() => store.state.filtered);
const solutionSearchFilter = ref('');
const typeSearchFilter = ref('');

const asset = ref({});
const solutions = ref([]);
const solutionsBySol = ref([]);
const solutionsByType = ref([]);
const darkMode = ref('')

//get all solutions from the chosen asset
const getSolutions = async () => {
    try {
        const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedSolutionsByAsset/${asset.value.assetName}`);
        solutions.value = response.data;
    } catch (err) {
        console.log(err.response.statusText);
    }
}

// update search term
const updateSearchTerm = async () => {
    solutionSearchFilter.value = '';
    await getSolutions();
}

// update filter term
const updateFilterTerm = async () => {
    typeSearchFilter.value = '';
    await getSolutions();
}

const goToChosenSolution = (id) => {
    store.commit('setChosenSolution', id);
    store.commit('changeToSolution');
};

// get config item from id
const getItemById = async () => {
    try {
        const response = await Axios.get(
            `https://${clientsideConfig.url}:${clientsideConfig.port}/api/getConfigItem/${chosenAssetId.value}`
        );
        asset.value = response.data;
        await getSolutions();
    } catch (err) {
        console.log(err.response.statusText);
    }
}

//get all solutions based on the searched solution name
const searchSolution = async () => {
    if (solutionSearchFilter.value === '') {
        await getSolutions();
    } else {
        try {
            const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedSolutionsBySolution/${solutionSearchFilter.value}`);
            solutionsBySol.value = response.data;
            solutions.value = filterObjectsWithMatchingIds(solutions.value, solutionsBySol.value);
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

//get all solutions based on the searched solution name
const filterSolutionByType = async () => {
    if (typeSearchFilter.value === '') {
        await getSolutions();
    } else {
        try {
            const response = await Axios.get(`https://${clientsideConfig.url}:${clientsideConfig.port}/api/getSelectedSolutionsByType/${typeSearchFilter.value}`);
            solutionsByType.value = response.data;
            solutions.value = filterObjectsWithMatchingIds(solutions.value, solutionsByType.value);
        } catch (err) {
            console.log(err.response.statusText);
        }
    }
}

const filterObjectsWithMatchingIds = (arr1, arr2) => {
    return arr1.filter(obj1 => {
        return arr2.some(obj2 => obj2.primaryID === obj1.primaryID);
    });
};

watch(searchable, updateSearchTerm);
watch(filtered, updateFilterTerm);

const getSession = async () => {
    const loggedInUserDarkModeBool = getItem('logged-in-user-darkMode');
    if (loggedInUserDarkModeBool == 1) {
        darkMode.value = true;
    } else {
        darkMode.value = false;
    }
}

function getItem(item) {
    if (process.client) {
        return localStorage.getItem(item)
    } else {
        return undefined
    }
}

watch(modeChanged, getSession)

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

<script>
export default {
    name: "AssetSolutionList",
};
</script>


<style scoped>
.data {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    align-self: stretch;
    width: 100%;
    padding: 1.25rem 1.875rem;
    gap: 1.25rem;
    border-radius: 0.625rem;
    box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

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

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

.solutionLabel {
    width: 5.5625em;
    height: 1.875em;
    font-family: "Overpass";
    font-style: normal;
    font-weight: 400;
    font-size: 0.875em;
    line-height: 1.875;
    letter-spacing: 0.05em;
}

.nuxt-link-darkmode {
    color: white;
}

.nuxt-link-lightmode {
    color: #000;
}

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

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

.dataInput {
    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;
}

.input {
    border: none;
}

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

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

.solution-search {
    display: flex;
    align-items: center;
    padding: 0.625em 1.875em;
    gap: 1.25em;
    width: 30.125em;
    height: 3.125em;
    box-shadow: 0.25em 0.25em 0.25em rgba(0, 0, 0, 0.25);
    border-radius: 0.625em;
    box-sizing: border-box;
}

.data-table {
    width: 100%;
    padding: 0 0.625rem;
    table-layout: fixed;
    border-collapse: collapse;
}

.table-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 3.125rem;
    padding: 0.625rem;
    gap: 0.625rem;
}

#nuxt-link {
    text-decoration: none;
}

.tr-head-darkmode {
    border-top: none;
    border-bottom: 0.0625rem solid #000000;
}

.tr-head-lightmode {
    border-top: none;
    border-bottom: 0.0625rem solid #8e8e8e;
}

.tr-darkmode {
    border-top: 0.0625rem solid #000000;
}

.tr-lightmode {
    border-top: 0.0625rem solid #8e8e8e;
}

th,
td {
    height: 1.875rem;
    text-align: left;
    padding: 0;
    letter-spacing: 5%;
    font: 400 0.875rem/1.875rem Overpass, sans-serif;
}

th {
    font: 700 0.875rem/1.875rem Overpass, sans-serif;
}

.th-darkmode,
.td-darkmode {
    color: #ffffff;
}

.th-lightmode,
.td-lightmode {
    color: #000;
}

.Solution {
    width: 50%;
}

.Solution-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Solution-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.Asset {
    width: 22%;
}

.Asset-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Asset-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.Task {
    width: 35%;
}

.Task-darkmode {
    border-right: 0.0625rem solid #000000;
}

.Task-lightmode {
    border-right: 0.0625rem solid #8e8e8e;
}

.Type {
    width: 35%;
}

.label {
    width: 6.0625em;
    height: 1.875em;
    font-family: 'Overpass';
    font-style: normal;
    font-weight: 400;
    font-size: 1em;
    line-height: 1.875em;
    letter-spacing: 0.05em;
}

.label-darkmode {
    color: #FFFFFF;
}

.label-lightmode {
    color: #000;
}
</style>