<script setup>
import { ref, watch, nextTick } from 'vue'
import { useFillHeight } from '../../composables/useFillHeight'
import { useExtension } from '../../composables/useExtension'
import { useLocalStorage } from '../../composables/useLocalStorage'
import ExtensionStatus from './common/ExtensionStatus.vue'

const clipboardContent = useLocalStorage('clipboard-sniffer-content', '')
const textareaRef = ref(null)

const {
  isExtensionReady,
  isListening,
  lastClipboardText,
  startListening,
  stopListening
} = useExtension()

const { height: textareaHeight } = useFillHeight(textareaRef, 40)

// Watch for clipboard updates from extension
watch(lastClipboardText, (newText) => {
  if (newText) {
    clipboardContent.value += (clipboardContent.value ? '\n' : '') + newText
    scrollToBottom()
  }
})

const scrollToBottom = () => {
  nextTick(() => {
    const textarea = document.querySelector('.tool-textarea')
    if (textarea) {
      textarea.scrollTop = textarea.scrollHeight
    }
  })
}

const copyToClipboard = () => {
  if (clipboardContent.value) {
    navigator.clipboard.writeText(clipboardContent.value)
  }
}

const clearText = () => {
  clipboardContent.value = ''
}
</script>

<template>
  <div class="tool-container full-width">
    <div class="tool-panel">
      <div class="tool-header">
        <h2 class="tool-title">Clipboard Sniffer</h2>
        <div class="extension-indicator-wrapper">
          <ExtensionStatus :isReady="isExtensionReady" />
        </div>
      </div>

      <div class="controls">
        <button
          v-if="!isListening"
          class="btn-neon"
          @click="startListening"
          :disabled="!isExtensionReady"
          :title="!isExtensionReady ? 'Extension required' : 'Start capturing clipboard'"
          v-ripple
        >
          Start Sniffing
        </button>
        <button
          v-else
          class="btn-neon danger"
          @click="stopListening"
          v-ripple
        >
          Stop Sniffing
        </button>

        <button class="btn-neon" @click="copyToClipboard" v-ripple>
          Copy
        </button>

        <button class="btn-neon" @click="clearText" v-ripple>
          Clear
        </button>
      </div>

      <div class="result-area" style="margin-top: 1rem;">
        <div ref="textareaRef" :style="{ height: textareaHeight, width: '100%' }">
          <textarea
            v-model="clipboardContent"
            class="tool-textarea"
            placeholder="Clipboard content will appear here line by line..."
          ></textarea>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.tool-header {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
}

.controls {
  display: flex;
  justify-content: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.tool-container.full-width {
  max-width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.tool-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  gap: 1.5rem;
}

.tool-textarea {
  width: 100%;
  height: 100%;
  padding: 1rem;
  background-color: var(--toggle-bg);
  border: 1px solid var(--toggle-border);
  border-radius: 8px;
  color: var(--text-color);
  font-family: monospace;
  resize: none;
  font-size: 0.9rem;
  line-height: 1.5;
}

.tool-textarea:focus {
  outline: none;
  border-color: var(--primary-accent);
}

.extension-indicator-wrapper {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
}
</style>
