<template>
    <div>
        <div class="vcd-signal-title">{{ t('signal') }}({{signals.content.length}})</div>
        <hr>
        <el-scrollbar height="86vh" class="vcd-signal-signals-display">
            <div v-for="(signal, index) in signals.content" :key="index"
                @click="toggleRender($event, signal)"
                class="vcd-signal-signal-item"
                :class="globalLookup.currentWires.has(signal) ? 'vcd-treeview-selected' : ''"
            >
                <div class="vcd-signal-signal-item-text"><span :class="`iconfont ${makeIconClass(signal)}`"></span>&ensp;{{ signal.name }}</div>
                <div>
                    <div :class="signal.size > 1 ? 'vcd-signal-signal-caption' : ''">
                        {{ makeSignalCaption(signal) }}
                    </div>
                </div>
            </div>
        </el-scrollbar>
    </div>
</template>

<script setup>
import { defineComponent, reactive } from 'vue';
import { useI18n } from 'vue-i18n';

import { emitter, globalLookup } from '@/hook/global';
import { makeIconClass } from '@/hook/utils';
import { WaveContainerView } from '@/hook/wave-container-view';
import { controller } from './signals';
import { saveViewApi } from '@/api';
import { makeSaveViewPayload } from '@/hook/recover';

defineComponent({ name: 'signals' });

const { t } = useI18n();

// signal : link, name, size, type
const signals = reactive({
    /**
     * @type {WireItem[]}
     */
    content: []
});

emitter.on('tree-view', sendWires => {
    signals.content = sendWires;
});

function makeSignalCaption(signal) {
    return signal.size === 1 ? '' : `${signal.size - 1}:0`;
}

/**
 * @description 用于点击波形后触发的逻辑，如果波形已经在列表中，则去除，否则，加入
 * NOTE：该函数需要同时操作所有的波形容器视图
 * @param {MouseEvent} event
 * @param {WireItem} signal
*/
function toggleRender(event, signal) {
    const lastSignal = controller.lastSignal;

    // 按下 shift 是连续选中 
    if (event.shiftKey && lastSignal !== undefined) {
        const renderSignals = [];
        let addBegin = false;
        for (const currentSignal of signals.content) {
            if (currentSignal === signal || lastSignal === currentSignal) {
                renderSignals.push(currentSignal);
                addBegin = !addBegin;
            } else if (addBegin) {
                renderSignals.push(currentSignal);
            }
        }
 
        for (const renderSignal of renderSignals) {
            WaveContainerView.add(renderSignal);
            controller.lastSignal = renderSignal;
        }
        globalLookup.render();

    } else {
        if (WaveContainerView.has(signal)) {
            WaveContainerView.delete(signal);
            controller.lastSignal = undefined;
            globalLookup.render();
        } else {
            WaveContainerView.add(signal);
            controller.lastSignal = signal;
            globalLookup.render();
        }
    }

    // 保存
    const savePath = globalLookup.originFile + '.view';
    const payload = makeSaveViewPayload();    
    saveViewApi(savePath, payload);
}
</script>

<style>
.vcd-signal-title {
    margin-left: 5px;
    height: 2.5vh;
}

.icon-wave-square {
    color: var(--signal-default-color);
}

.icon-brackets {
    color: #75BEDF;
}

.icon-register {
    color:#885dff;
}

.icon-integer {
    color: #C76B3C;
}

.icon-string {
    color: #8CC265;
}

.icon-real {
    color: #5fb4d8;
}

.icon-task {
    color: orange;
}

.vcd-signal-signals-display {
    color: var(--sidebar-item-text);
    padding: 0px 8px;
}


.vcd-signal-signal-item {
    margin: 3px;
    display: flex;
    justify-content: space-between;
    height: 23px;
    padding-left: 3px;
    cursor: pointer;
    align-items: center;
    font-size: 0.9rem;
}


.vcd-signal-signal-item::selection {
    background: none;
}

.vcd-signal-signal-item:hover {
    background-color: var(--sidebar-item-selected);
    border-radius: .3em;
}

.vcd-signal-signal-caption {
    color: var(--sidebar-item-text);
    border-radius: .5em;
    background-color: var(--color-deepPurple);
    padding: 3px;
    font-size: 12px;
}

.vcd-signal-signal-item-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 200px;
}

.vcd-signal-signal-item-text:hover {
    overflow: unset;
    text-overflow: unset;
}

</style>