<template>
  <div
    class="clipboard-container"
    @mouseenter="mouseEntering = true"
    @mouseleave="mouseEntering = false"
  >
    <Transition name="fade">
      <img
        class="copy-icon"
        v-if="!mouseEntering"
        src="/static/copy.png"
        alt="copy"
      />
      <div v-else>
        <div class="clipboard-header">
          <div class="clipboard-actions">
            <Button @click="getRemoteClipboard" :loading="loading">{{
              $t("clipboard.getRemoteClipboard")
            }}</Button>
          </div>
        </div>
        <div class="clipboard-content">
          <div class="clipboard-text">
            {{ remoteClipboard || $t("clipboard.noContent") }}
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { Button } from "ant-design-vue";
import { notification } from "ant-design-vue";
import { peer, MessageType } from "../utils/peer";

const { t } = useI18n();

const loading = ref(false);
const remoteClipboard = ref("");
const mouseEntering = ref(false);

// 获取远程粘贴板内容
const getRemoteClipboard = async () => {
  loading.value = true;
  try {
    const result = await peer.send({
      type: MessageType.request_copyClipboard,
      data: null,
    });
    remoteClipboard.value = result;
    notification.success({
      message: t("clipboard.fetchSuccess"),
    });
  } catch (error: any) {
    notification.error({
      message: t("clipboard.fetchFailed"),
      description: error?.message || t("clipboard.unknownError"),
    });
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition:
    opacity 0.5s ease,
    max-height 0.5s ease;
  max-height: 200px;
  overflow: hidden;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  max-height: 0;
  overflow: hidden;
}

.clipboard-container {
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  max-width: 180px;
}

.clipboard-header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
}

.clipboard-header h3 {
  margin: 0;
}

.clipboard-actions {
  display: flex;
  justify-content: center;
  align-items: center;
}

.clipboard-content::-webkit-scrollbar {
  display: none;
}
.clipboard-content::-webkit-scrollbar-thumb {
  display: none;
}

.clipboard-text {
  background: white;
  padding: 10px;
  border-radius: 4px;
  min-height: 60px;
  word-break: break-all;
  white-space: pre-wrap;
  font-size: 14px;
  color: #666;
  text-align: center;
}
.copy-icon {
  width: 20px;
  height: 20px;
  position: absolute;
}
</style>
