<script>
export default {
  inheritAttrs: false
}
</script>

<script setup>
import { ref, watch, onUnmounted } from 'vue'
import { Plug, Plus, X } from 'lucide-vue-next'

const props = defineProps({
  isReady: Boolean
})

const showModal = ref(false)

const handleKeydown = (e) => {
  if (e.key === 'Escape' && showModal.value) {
    showModal.value = false
  }
}

watch(showModal, (isOpen) => {
  if (isOpen) {
    window.addEventListener('keydown', handleKeydown)
  } else {
    window.removeEventListener('keydown', handleKeydown)
  }
})

onUnmounted(() => {
  window.removeEventListener('keydown', handleKeydown)
})
</script>

<template>
  <div class="extension-status" v-bind="$attrs" :class="{ 'is-ready': isReady }" @click="showModal = true" :title="isReady ? 'Extension Connected' : 'Extension Not Connected'">
    <Plug v-if="isReady" size="18" />
    <Plus v-else size="18" />
  </div>

  <Teleport to="body">
    <div v-if="showModal" class="modal-overlay" @click.self="showModal = false">
      <div class="modal-content glass-panel">
        <button class="close-btn" @click="showModal = false">
          <X size="24" />
        </button>

        <div v-if="!isReady">
          <h3>Enhance Your Experience</h3>
          <p>
            Install our browser extension to enable <strong>background clipboard features</strong>!
          </p>
          <p class="description">
            Without the extension, this tool can only access clipboard when the tab is active.
            With the extension, you can capture and process content even when you're working in other apps.
          </p>
          <div class="modal-actions">
            <a
              href="https://chromewebstore.google.com/detail/tools-app-extension/bhcpbmfncohogehbhebiffcgjcndnneg"
              target="_blank"
              rel="noopener noreferrer"
              class="btn-neon"
            >
              Install Extension
            </a>
          </div>
        </div>

        <div v-else>
          <h3>Extension Connected!</h3>
          <p>
            You have successfully enabled <strong>background clipboard integration</strong>.
          </p>
          <p class="description">
            The extension is active and ready to process your clipboard in the background.
          </p>
          <div class="modal-actions">
            <button class="btn-neon" @click="showModal = false">Got it!</button>
          </div>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<style scoped>
.extension-status {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.3s ease;
}

:global(:root[data-theme="light"]) .extension-status {
  background: rgba(0, 0, 0, 0.05);
  color: var(--text-secondary);
}

.extension-status:hover {
  background: rgba(255, 255, 255, 0.2);
  color: var(--primary-accent);
}

:global(:root[data-theme="light"]) .extension-status:hover {
  background: rgba(0, 0, 0, 0.1);
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
  padding: 1rem;
}

.modal-content {
  background: var(--glass-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  padding: 2.5rem; /* Większy padding */
  max-width: 500px;
  width: 90%;
  position: relative;
  box-shadow: var(--glass-shadow);
  text-align: center;
  color: var(--text-color); /* Wymuś kolor tekstu */
}

.close-btn {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0.5rem;
  transition: color 0.2s;
}

.close-btn:hover {
  color: var(--text-color);
}

h3 {
  margin-top: 0;
  margin-bottom: 1.5rem;
  font-size: 1.8rem; /* Większy nagłówek */
  background: var(--title-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 700;
}

p {
  font-size: 1.1rem;
  line-height: 1.6;
  margin-bottom: 1rem;
  color: var(--text-color); /* Wymuś kolor */
}

.description {
  color: var(--text-secondary);
  margin-bottom: 2rem;
  font-size: 1rem;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-top: 2rem;
}

.modal-actions .btn-neon {
  font-size: 1.1rem;
  padding: 0.8rem 2.5rem;
  min-width: 140px;
}

strong {
  color: var(--primary-accent);
  font-weight: 600;
}

.extension-status.is-ready {
  color: #4ade80;
  background: rgba(74, 222, 128, 0.1);
}

.extension-status.is-ready:hover {
  background: rgba(74, 222, 128, 0.2);
  color: #4ade80;
}

:global(:root[data-theme="light"]) .extension-status.is-ready {
  background: rgba(74, 222, 128, 0.15);
  color: #16a34a; /* Darker green for light mode */
}
</style>
