<script setup>
import { ref } from 'vue';
import { useFillHeight } from '../../composables/useFillHeight';

// Options
const useLower = ref(true);
const useUpper = ref(true);
const useDigits = ref(true);
const useSpecial = ref(false);
const skipSimilar = ref(true);
const length = ref(16);
const count = ref(20);

// Result
const result = ref('');
const textareaRef = ref(null);
const { height: textareaHeight } = useFillHeight(textareaRef, 40);

// Character Sets
const CHARS_LOWER = 'abcdefghijklmnopqrstuvwxyz';
const CHARS_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const CHARS_DIGITS = '0123456789';
const CHARS_SPECIAL = '!@#$%^&*()_+~`|}{[]:;?><,./-=';
const CHARS_SIMILAR = /[Il1O0o]/g;

const generatePasswords = () => {
  let charset = '';
  if (useLower.value) charset += CHARS_LOWER;
  if (useUpper.value) charset += CHARS_UPPER;
  if (useDigits.value) charset += CHARS_DIGITS;
  if (useSpecial.value) charset += CHARS_SPECIAL;

  if (skipSimilar.value) {
    charset = charset.replace(CHARS_SIMILAR, '');
  }

  if (!charset) {
    result.value = 'Please select at least one character set.';
    return;
  }

  const passwords = [];
  const charsetLength = charset.length;

  // We need (count * length) random values.
  // However, crypto.getRandomValues has a limit on array size (65536 bytes).
  // So we'll generate one random value per character needed in a loop or batch.
  // For simplicity and safety, let's generate per password or per character if needed.
  // The most robust way for "bulk" is to just loop.

  for (let i = 0; i < count.value; i++) {
    let password = '';
    // Create a typed array for the random values for this password
    const randomValues = new Uint32Array(length.value);
    crypto.getRandomValues(randomValues);

    for (let j = 0; j < length.value; j++) {
      // Use modulo to map the random 32-bit integer to a charset index
      // Note: This introduces a tiny bias if charsetLength is not a power of 2,
      // but for typical password generation it is negligible compared to Math.random().
      // For cryptographic perfection, rejection sampling would be needed,
      // but this is standard practice for password managers.
      const randomIndex = randomValues[j] % charsetLength;
      password += charset[randomIndex];
    }
    passwords.push(password);
  }

  result.value = passwords.join('\n');
};
</script>

<template>
  <div class="tool-container full-width">
    <div class="tool-panel">
      <div class="panel-header">
        <h2 class="tool-title">Bulk Passwords Generator</h2>
        <div class="action-area desktop-only">
          <button class="btn-neon generate-btn" @click="generatePasswords" v-ripple>
            Generate
          </button>
        </div>
      </div>

      <div class="options-grid">
        <div class="checkbox-group">
          <label class="checkbox-label">
            <input type="checkbox" v-model="useLower">
            <span class="checkmark"></span>
            Lower Case (a-z)
          </label>
          <label class="checkbox-label">
            <input type="checkbox" v-model="useUpper">
            <span class="checkmark"></span>
            Upper Case (A-Z)
          </label>
          <label class="checkbox-label">
            <input type="checkbox" v-model="useDigits">
            <span class="checkmark"></span>
            Digits (0-9)
          </label>
          <label class="checkbox-label">
            <input type="checkbox" v-model="useSpecial">
            <span class="checkmark"></span>
            Special Chars
          </label>
          <label class="checkbox-label">
            <input type="checkbox" v-model="skipSimilar">
            <span class="checkmark"></span>
            Skip Similar (I, l, 1, O, 0, o)
          </label>
        </div>

        <div class="inputs-group">
          <div class="input-wrapper">
            <label>Length</label>
            <div class="number-control">
              <button class="control-btn" @click="length > 4 ? length-- : null">-</button>
              <input type="number" v-model="length" min="4" max="128" class="number-input">
              <button class="control-btn" @click="length < 128 ? length++ : null">+</button>
            </div>
          </div>
          <div class="input-wrapper">
            <label>Count</label>
            <div class="number-control">
              <button class="control-btn" @click="count > 1 ? count-- : null">-</button>
              <input type="number" v-model="count" min="1" max="1000" class="number-input">
              <button class="control-btn" @click="count < 1000 ? count++ : null">+</button>
            </div>
          </div>
        </div>
      </div>

      <div class="mobile-only" style="margin-top: 1rem; width: 100%;">
        <button class="btn-neon generate-btn" @click="generatePasswords" v-ripple style="width: 100%;">
          Generate
        </button>
      </div>

      <div class="result-area" :style="{ height: textareaHeight }">
        <textarea
          class="tool-textarea"
          v-model="result"
          placeholder="Generated passwords will appear here..."
          readonly
        ></textarea>
      </div>
    </div>
  </div>
</template>

<style scoped>
.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;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.tool-title {
  margin: 0;
}

.options-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 2rem;
  padding: 1rem;
  background: var(--toggle-bg);
  border: 1px solid var(--toggle-border);
  border-radius: 12px;
}

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  flex: 2;
}

.inputs-group {
  display: flex;
  gap: 1rem;
  flex: 1;
  min-width: 200px;
  flex-wrap: wrap;
}

.input-wrapper {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1;
  min-width: 140px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
  color: var(--text-secondary);
  font-weight: 500;
  white-space: nowrap;
}

/* Custom Checkbox */
.checkbox-label input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkmark {
  position: relative;
  height: 20px;
  width: 20px;
  background-color: var(--toggle-bg);
  border: 1px solid var(--toggle-border);
  border-radius: 4px;
  transition: all 0.3s ease;
}

.checkbox-label:hover .checkmark {
  border-color: var(--toggle-hover-border);
}

.checkbox-label input:checked ~ .checkmark {
  background-color: var(--primary-accent);
  border-color: var(--primary-accent);
  box-shadow: 0 0 10px var(--primary-accent);
}

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

.checkbox-label input:checked ~ .checkmark:after {
  display: block;
}

.checkbox-label .checkmark:after {
  left: 6px;
  top: 2px;
  width: 5px;
  height: 10px;
  border: solid #000;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

/* Number Control */
.number-control {
  display: flex;
  align-items: stretch;
  background: var(--toggle-bg);
  border: 1px solid var(--toggle-border);
  border-radius: 8px;
  overflow: hidden;
  gap: 0;
}

.control-btn {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 1.2rem;
  width: 40px;
  height: auto;
  min-height: 40px;
  cursor: pointer;
  transition: background 0.2s;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.control-btn:hover {
  background: var(--button-hover-bg);
}

.number-input {
  background: none;
  border: none;
  color: var(--text-color);
  width: 100%;
  flex: 1;
  text-align: center;
  font-size: 1rem;
  font-weight: bold;
  appearance: textfield;
  -moz-appearance: textfield;
  height: 100%;
  border-radius: 0;
  min-width: 60px;
}

.number-input:focus {
  outline: none;
  box-shadow: none;
  background: rgba(0, 0, 0, 0.05);
}

.number-input::-webkit-outer-spin-button,
.number-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.result-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 200px;
}

.tool-textarea {
  width: 100%;
  height: 100%;
  padding: 1rem;
  border-radius: 12px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  color: var(--text-color);
  font-family: monospace;
  font-size: 0.9rem;
  resize: none;
  outline: none;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.generate-btn {
  padding: 0.75rem 2rem;
  font-size: 1rem;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.desktop-only {
  display: block;
}

.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .options-grid {
    flex-direction: column;
    gap: 1rem;
  }

  .inputs-group {
    flex-direction: column;
    min-width: 100%;
  }

  .panel-header {
    flex-direction: column;
    align-items: stretch;
  }

  .generate-btn {
    width: 100%;
  }

  .desktop-only {
    display: none;
  }

  .mobile-only {
    display: block !important;
  }
}
</style>
