<template>
  <div v-if="toggle?.hasContent" class="control-item" :style="{ color: textColor }">
    <div>
      <h1>Toggle is: {{ toggle.value ? 'On' : 'Off' }}</h1>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useBackgroundElementDesign, useToggleElementContent } from '@lightspeed/crane';
import { Content, Design } from '../../type.ts';
import { isDark } from '../../../../shared/utils.ts';

const toggle = useToggleElementContent<Content>('toggle');
const backgroundDesign = useBackgroundElementDesign<Design>('background');

const backgroundColor = computed(() => {
  const currentColor = backgroundDesign.background?.type === 'solid' ? backgroundDesign.background?.solid?.color?.hex
    : backgroundDesign.background?.gradient?.fromColor?.hex;
  return currentColor || '#ffffff';
});

const textColor = computed(() => (isDark(backgroundColor.value) ? 'white' : 'black'));
</script>

<style lang="scss" scoped>
.custom-block {
  padding: 50px;
  background-color: #f8f8f8;
  border-radius: 8px;

  @media screen and (min-width: 700px) {
    padding: 75px;
  }
  @media screen and (min-width: 1200px) {
    padding: 100px;
  }

  &__blank_space {
    height: 40px;
  }
}

.control-item {
  margin: 20px 0;
  display: flex;
  align-items: center;
  gap: 10px;

  label {
    font-weight: 500;
  }

  select {
    padding: 8px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
  }
}

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;

  input {
    display: none;
  }

  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: 0.4s;
    border-radius: 20px;
  }

  input:checked + .slider {
    background-color: #4caf50;
  }

  .slider::before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    background-color: white;
    border-radius: 50%;
    top: 3px;
    left: 3px;
    transition: 0.4s;
  }

  input:checked + .slider::before {
    transform: translateX(20px);
  }
}

.highlight {
  color: #4caf50;
  font-weight: bold;
}
</style>
