<template>
  <div class="btn-content">
    <Popover 
      v-if="isPC"
      color="rgba(107, 117, 138, 0.5)"
      :show="isShowPopover"
      @hover="handleMouseEnter"
      trigger="hover"
    >
      <template #content>
        <DeviceSelect
          :isShowControlBtn='true'
          :deviceType="DeviceType.MICROPHONE"/>
      </template>
      <template #trigger>
        <Button 
          :iconSrc="config.iconSrc"
          :color="config.color"
          :iconSize="config.iconSize"
          :width="config.width"
          :height="config.height"
          :shape="config.shape"
          @click="handleClick"
        >
        </Button>
      </template>
    </Popover>
    <Button
      v-if="!isPC"
      :iconSrc="config.iconSrc"
      :color="config.color"
      :iconSize="config.iconSize"
      :width="config.width"
      :height="config.height"
      :shape="config.shape"
      @click="handleClick"
    >
    </Button>
    <TKText
      v-if="showText"
      :textStyle="config.textStyle"
      :color="config.textColor"
      :size="config.textSize"
    >
      {{ btnText }}
    </TKText>
  </div>
</template>

<script lang="ts">
export default {
  options: {
    virtualHost: true,
  },
};
</script>

<script lang="ts" setup>
import { computed, toRefs } from '../../../../adapter-vue';
import { TUICallKitServer, TUIGlobal, t } from '../../../../TUICallService/index';
import { DeviceType } from '../../../../TUICallService/const';
import Button from '../../base/Button/Button.vue';
import TKText from '../../base/TKText/TKText.vue';
import Popover from '../../base/Popover/Popover.vue';
import DeviceSelect from '../DeviceSelect/DeviceSelect.vue';
import { PopoverContextValue } from '../../../context';
import { useUserInfoExcludeVolumeContext, usePopover } from '../../../hooks';
import { ButtonProps } from './props/Button';
import { useBtnConfig } from './hooks/useConfig'

defineProps(ButtonProps);
const isPC = TUIGlobal.isPC;
const { localUserInfoExcludeVolume : localUserInfo } = toRefs(useUserInfoExcludeVolumeContext());
const popoverValue = usePopover();
const isShowPopover = computed(() => popoverValue.value === 'microphone');
const configState = computed(() => (localUserInfo?.value.isAudioAvailable ? 'basicConfig' : 'closedConfig'));
const config = useBtnConfig('microphone', configState);
const btnText = computed(() => (localUserInfo?.value.isAudioAvailable ? t('microphone enabled') : t('microphone disabled')));
const handleClick = async () => {
  if (localUserInfo?.value.isAudioAvailable) {
    await TUICallKitServer.closeMicrophone();
  } else {
    await TUICallKitServer.openMicrophone();
  }
};
const handleMouseEnter = () => {
  PopoverContextValue.value = 'microphone';
};
</script>

<style lang="scss" scoped>
@import './style/common.scss';
</style>
