<template>
  <div
    v-if="visible"
    id="page-popup"
    :style="{ top: position.y + 'px', left: position.x + 'px' }"
  >
    <div class="popup-header">
      <span>{{ pageSelector }}</span>
      <button class="close-btn" @click="close">×</button>
    </div>
    <div class="popup-body">
      <div class="popup-controls">
        <p class="info">Gabarit de page</p>
        <!-- Additional page-specific controls will be added here later -->
      </div>
      <div class="popup-css">
        <textarea
          :value="pageCss"
          @input="handleCssInput"
          spellcheck="false"
        ></textarea>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStylesheetStore } from '../stores/stylesheet';

const stylesheetStore = useStylesheetStore();

const props = defineProps({
  iframeRef: Object,
});

const visible = ref(false);
const position = ref({ x: 0, y: 0 });
const pageSelector = ref('@page');
const selectedPageElement = ref(null);

const calculatePosition = (pageElement) => {
  const rect = pageElement.getBoundingClientRect();
  const iframeRect = props.iframeRef.getBoundingClientRect();
  return {
    x: iframeRect.left + rect.left + 20,
    y: iframeRect.top + rect.top + 20,
  };
};

const open = (pageElement) => {
  selectedPageElement.value = pageElement;
  pageSelector.value = '@page';
  position.value = calculatePosition(pageElement);

  // Add border to the selected page
  pageElement.style.outline = '2px solid #61afef';

  visible.value = true;
};

const close = () => {
  // Remove border from the selected page
  if (selectedPageElement.value) {
    selectedPageElement.value.style.outline = '';
    selectedPageElement.value = null;
  }
  visible.value = false;
};

const pageCss = computed(() => {
  return stylesheetStore.extractBlock('@page') || '';
});

let cssDebounceTimer = null;

const handleCssInput = (event) => {
  const newCss = event.target.value;

  if (cssDebounceTimer) {
    clearTimeout(cssDebounceTimer);
  }

  cssDebounceTimer = setTimeout(() => {
    const oldBlock = pageCss.value;
    if (oldBlock) {
      stylesheetStore.content = stylesheetStore.content.replace(oldBlock, newCss);
    }
  }, 500);
};

defineExpose({ open, close, visible });
</script>

<style scoped>
#page-popup {
  position: fixed;
  background: white;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 10000;
  min-width: 400px;
  max-width: 500px;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #eee;
  font-size: 0.875rem;
  font-weight: bold;
  background: #f5f5f5;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
  padding: 0;
}

.popup-body {
  display: flex;
  gap: 1px;
  background: #eee;
}

.popup-controls {
  flex: 1;
  padding: 0.75rem;
  background: white;
}

.popup-css {
  flex: 1;
  background: #1e1e1e;
  display: flex;
}

.popup-css textarea {
  width: 100%;
  height: 200px;
  background: #1e1e1e;
  color: #abb2bf;
  border: none;
  padding: 0.75rem;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.75rem;
  line-height: 1.4;
  resize: none;
  outline: none;
}

.info {
  font-size: 0.875rem;
  color: #666;
  margin: 0 0 0.5rem 0;
}
</style>
