<template>
  <section class="locations-section py-5">
   <div class="container-fluid container-text">
     <div class="row align-items-center mb-5">
       <div class="col-md-6 text-column">
         <transition name="text-pop" appear>
           <h2 class="locations-title">
             {{ t('locations_title') }}
           </h2>
         </transition>
         <transition name="fade-slide" appear>
           <p class="locations-text">
             {{ t('locations_text_1') }}
           </p>
         </transition>
         <transition name="fade-slide" appear>
           <p class="locations-text">
             {{ t('locations_text_2') }}
           </p>
         </transition>
         <transition name="fade-slide" appear>
           <p class="locations-text">
             {{ t('locations_text_3') }}
           </p>
         </transition>
       </div>
       <div class="col-md-6 text-center text-md-end">
         <img
           ref="forestRef"
           :src="forestImage"
           alt="Forest Ln"
           class="img-fluid  mb-4 location-img"
           :class="{ highlighted: props.highlight === 'forest' }"
           @click="openModal(forestImage)"
         />
         <img
           ref="springRef"
           :src="springImage"
           alt="Spring Valley"
           class="img-fluid location-img"
           :class="{ highlighted: props.highlight === 'spring' }"
           @click="openModal(springImage)"
         />
       </div>
     </div>
   </div>

   <transition name="modal-fade">
      <div v-if="showModal" class="modal-overlay" @click="closeModal">
        <transition name="zoom-fade">
          <div class="modal-content">
            <button class="close-btn" @click="closeModal">×</button>
            <div class="modal-img-wrapper">
              <img :src="modalImage" alt="Ubicación" class="modal-img" />
            </div>
          </div>
        </transition>
      </div>
    </transition>
 </section>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

const props = defineProps<{ highlight: string }>(); 

const forestRef = ref<HTMLImageElement | null>(null);
const springRef = ref<HTMLImageElement | null>(null);

const showModal = ref(false);
const modalImage = ref('');

const forestImage = new URL('@/assets/Resources/AboutUs/locations1.png', import.meta.url).href;
const springImage = new URL('@/assets/Resources/AboutUs/locations2.png', import.meta.url).href;

let pulseInterval: number | null = null;
let animationCheckInterval: number | null = null;

function applyPulseEffect(element: HTMLElement | null) {
  if (!element) return null;
  let intensity = 0;
  let increasing = true;
  
  const pulse = () => {
    if (increasing) {
      intensity += 0.05;
      if (intensity >= 0.8) increasing = false;
    } else {
      intensity -= 0.05;
      if (intensity <= 0.3) increasing = true;
    }
    element.style.boxShadow = `0 0 ${intensity * 25}px rgba(100,171,30,${intensity})`;
    // Asegurar que los estilos de highlighted permanezcan
    element.style.border = '3px solid #64ab1e';
    
    
  };
  
  pulse();
  return setInterval(pulse, 100) as unknown as number;
}

function ensureHighlighted() {
  if (props.highlight === 'forest' && forestRef.value) {
    forestRef.value.classList.add('highlighted');
    if (springRef.value) springRef.value.classList.remove('highlighted');
    if (!pulseInterval) {
      pulseInterval = applyPulseEffect(forestRef.value);
    }
  } else if (props.highlight === 'spring' && springRef.value) {
    springRef.value.classList.add('highlighted');
    if (forestRef.value) forestRef.value.classList.remove('highlighted');
    if (!pulseInterval) {
      pulseInterval = applyPulseEffect(springRef.value);
    }
  }
}

onMounted(() => {
  ensureHighlighted();
  // Verificar periódicamente que la clase highlighted esté aplicada
  animationCheckInterval = setInterval(() => {
    ensureHighlighted();
  }, 1000); // Cada segundo
});

onUnmounted(() => {
  if (pulseInterval) {
    clearInterval(pulseInterval);
  }
  if (animationCheckInterval) {
    clearInterval(animationCheckInterval);
  }
});

// Observar cambios en la prop highlight
watch(() => props.highlight, (newHighlight, oldHighlight) => {
  if (newHighlight !== oldHighlight) {
    if (pulseInterval) {
      clearInterval(pulseInterval);
      pulseInterval = null;
    }
    ensureHighlighted();
  }
});

function openModal(image: string) {
  modalImage.value = image;
  showModal.value = true;
}

function closeModal() {
  showModal.value = false;
}
</script>

<style scoped>
  .text-column {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-right: 2rem;
    
  }
  
  .locations-title {
    font-size: 2rem;
    font-weight: 700;
    color: #64ab1e;
    line-height: 1.2;
    margin-bottom: 1.5rem;
    letter-spacing: 0.5px;
    transition: all 0.3s ease-in-out;
  }
  .locations-title:hover {
    color: #343633;
    transition: color 0.3s ease, transform 0.3s ease;
    transform: scale(1.05);
  }
  
  .locations-text {
    font-size: 1.2rem;
    color: #333;
    line-height: 1.7;
    text-align: justify;
    margin-bottom: 1.25rem;
  }
  .container-text {
   padding-right:5rem;
   padding-left: 5rem;
  }
  
  @keyframes textPop {
    0% { opacity: 0; transform: scale(0.9) translateY(20px); }
    60% { opacity: 1; transform: scale(1.05) translateY(0); }
    100% { opacity: 1; transform: scale(1) translateY(0); }
  }
  .text-pop-enter-active {
    animation: textPop 0.8s ease-out;
  }
  
  @keyframes fadeSlide {
    0% { opacity: 0; transform: translateY(20px); }
    100% { opacity: 1; transform: translateY(0); }
  }
  .fade-slide-enter-active {
    animation: fadeSlide 0.6s ease-out;
  }
  
  @keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.3); }
    100% { transform: scale(1); }
  }
  @keyframes pulse {
    0% { box-shadow: 0 0 0 rgba(100,171,30,0.4); }
    50% { box-shadow: 0 0 20px rgba(100,171,30,0.7); }
    100% { box-shadow: 0 0 0 rgba(100,171,30,0.4); }
  }
  .highlighted {
    border: 3px solid #64ab1e;
    animation: pop 1s ease-out 1;
  }
  
  .location-img {
    max-height: 300px;
    object-fit: cover;
   
    width: 100%;
    transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
  }
 
  .location-img:hover {
    transform: scale(1.05);
    cursor: pointer;
    box-shadow: 0 0 20px rgba(100,171,30,0.7);
  }
  
/* Ajustes responsivos específicos para la sección de ubicaciones */

/* Pantallas medianas (tabletas) */
@media (max-width: 992px) {
  .locations-title {
    font-size: 1.8rem;
    margin-bottom: 1.2rem;
  }

  .locations-text {
    font-size: 1.1rem;
    margin-bottom: 1rem;
  }

  .location-img {
    max-height: 250px;
    margin-bottom: 1rem;
  }

  .container-text {
  padding-right: 3rem;
  padding-left: 3rem;
}
}
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1050;
  backdrop-filter: blur(3px);
  padding: 1rem;
}

.modal-content {
  position: relative;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  max-width: 90%;
  max-height: 90%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: transparent;
}

.modal-img-wrapper {
  width: 100%;
  height: auto;
  max-height: 85vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  border-radius: 20px;
}

.modal-img {
  width: 100%;
  height: auto;
  object-fit: contain;
  border-radius: 20px;
}



.close-btn {
  position: absolute;
  top: 15px;
  right: 15px;
  background: #343633;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 1.5rem;
  color: white;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background 0.3s ease;
  z-index: 2;
}

.close-btn:hover {
  background: #64ab1e;
}

.modal-fade-enter-active, .modal-fade-leave-active {
  transition: opacity 0.4s ease;
}
.modal-fade-enter-from, .modal-fade-leave-to {
  opacity: 0;
}

@media (max-width: 992px) {
  .modal-content {
    max-width: 95%;
    max-height: 80%;
  }
}

@media (max-width: 576px) {
  .modal-content {
    max-width: 95%;
    max-height: 75%;
    
  }

  .modal-overlay {
    align-items: start !important;
  }

  .close-btn {
    top: 10px;
    right: 10px;
    width: 28px;
    height: 28px;
    font-size: 1.2rem;
  }
}

@media (max-width: 2500px) {
  .modal-content {
    max-width: 900px;
    max-height: 700px;
  }  
}


@media (max-width: 768px) {

  
  
  .row.align-items-center {
    flex-direction: column-reverse;
  }

  .text-column {
    padding: 1rem;
    text-align: center;
  }

  .locations-title {
    font-size: 1.6rem;
  }

  .locations-text {
    font-size: 1rem;
    line-height: 1.6;
  }

  .location-img {
    max-height: 220px;
    margin-bottom: 1rem;
  }
}

/* Smartphones pequeños */
@media (max-width: 480px) {
  .locations-title {
    font-size: 1.4rem;
  }

  .locations-text {
    font-size: 0.9rem;
  }

  .location-img {
    max-height: 180px;
  }

  .highlighted {
   
    animation: pop 0.8s ease-out 1;
  }

  .container-text {
    padding-right: 1rem;
    padding-left: 1rem;
  }
}
</style>