<template>
    <div class="kf-3dc-atom kf-3dc-i-container w-100 h-100" :style="buttonStyle" ref="buttonBox" v-show="props.show" :class="'vivify '+ buttonClass"
         @click="doClick">
        <i class="kf-3dc-atom-i" :class="props.icon"></i>
    </div>
</template>

<script>
import {
    onMounted,
    ref,
    getCurrentInstance,
    computed,
    watch,
    nextTick,
} from "vue";
import styleManager from "../utils/style.vue";
import eventManager from "../utils/event";
import pageModel from "../model/page";
import dataModel from "@/components/ui/model/data";
import stateManager from "@/components/ui/utils/state";

export default {
    name: "kfIconButton",
    props: {
        props: {
            type: Object,
            default: () => {},
        },
        eventList: {
            type: Array,
            default: () => [],
        },
        eventScript: {
            type: String,
        },
    },
    widgetConfig: {
        parent: "base",
        rank: 5,
        label: {
            zh: Kf.t('IconButton')
        },
        category: Kf.t('Foundation'),
        component: "kfIconButton",
        display: true,
        version: 1,
        icon: "icon-anniu",
        defaultSize: {
            x: 32,
            y: 32,
        },
        imgName: "kfIconButton",
    },
    setup(props) {
        let bindDevice = null,
            bindModel = null,
            originValue = ref(false);

        let buttonBox = ref(null);
        const { proxy } = getCurrentInstance();
        let defaultHandler = ref({});

        let buttonStyle = computed(() => {
            return styleManager.computedStyle(proxy);
        });

        let buttonClass = computed(() => {
            return styleManager.computedClass(proxy);
        });

        onMounted(() => {
            refreshEvents();
        });
        const refreshEvents = function (firstTime = false) {
            eventManager.registerEvent(proxy);
        };

        pageModel.watchProps(props, watch, proxy, (changes) => {
            if (changes && changes["deviceId"]) {
                // 组件绑定的数据ID变化，需要请求新的设备数据和对应的物模型
                bindDevice = dataModel.getDataSourceByDeviceId(
                    props.props.deviceId
                );
                if (bindDevice) {
                    bindModel = dataModel.getDeviceModelByDevice(bindDevice);
                }
            }
        });

        let dataVersion = ref(0);

        const updateData = function () {
            dataVersion.value++;
        };

        const bindData = computed(() => {
            let value = "";
            if ((dataVersion.value > 0 || props.props.pageType===1) && props.props.deviceData) {
                let newData = dataModel.getData(props.props, false, true);
                if (newData !== undefined) {
                    originValue.value = newData;
                    value = newData;
                }
            }
            return value;
        });

        watch(bindData, (newValue, oldValue) => {
            nextTick(() => {
                stateManager.onDataChange(props, newValue);
            });
        });

        const updateDeviceAndModel = function (deviceId) {
            if (!deviceId) return;
            bindDevice = dataModel.getDataSourceByDeviceId(deviceId);
            if (bindDevice) {
                bindModel = dataModel.getDeviceModelByDevice(bindDevice);
            }
        };

        const doClick = function (event) {
            try {
                if (props.props.deviceId && props.props.deviceId != "") {
                    if (!bindModel) {
                        updateDeviceAndModel(props.props.deviceId);
                    }
                    let enumLabel = dataModel.getEnumLabelForValue(
                        props,
                        bindData.value,
                        bindModel
                    );
                    if (enumLabel) {
                        dataModel.doControl(
                            props,
                            bindDevice,
                            bindModel,
                            enumLabel
                        );
                    }
                }
                if (eventHandler.value?.click) {
                    eventHandler.value.click(event);
                }
            } catch (e) {
                console.error(e);
            }
        };

        const eventHandler = computed(() => {
            return eventManager.loadEventHandler(
                defaultHandler,
                undefined,
                props.props.pageName
            );
        });

        return {
            buttonBox,
            buttonStyle,
            doClick,
            defaultHandler,
            eventHandler,
            buttonClass,
            bindData,
            updateData,
        };
    },
};
</script>

<style lang="scss">
.kf-3dc-atom {
    border-width: 0;
    border-style: solid;
    padding: 4px;
    justify-content: center;
    align-items: center;
    transition: all 0.2s linear 0s;
    .kf-3dc-atom-i {
        width: 100%;
        height: 100%;
        background: none;
        border: none;
        display: flex;
        justify-content: inherit;
        align-items: center;
        color: inherit;
        font-size: inherit;
        border-radius: 0;
        font-family: inherit;
    }
}
</style>