<template>
  <div class="p-6 space-y-6 max-w-4/5 mx-auto">
    <h2 class="text-xl font-bold mb-12 m-6">Yourgold verification data decryptor</h2>

    <div class="space-y-4">
      <div class="flex justify-between items-center">
        <label class="text-sm ">Encrypted Zip File:</label>
        <button 
          @click="pickEncryptedFile" 
          class="bg-gray-200 px-4 py-2 rounded-md w-full sm:w-1/2 text-left overflow-hidden truncate text-black">
          {{ encryptedFile || 'Choose File' }}
        </button>
      </div>

      <div class="flex justify-between items-center">
        <label class="text-sm ">Private Key File:</label>
        <button 
          @click="pickPrivateKeyFile" 
          class="bg-gray-200 px-4 py-2 rounded-md w-full sm:w-1/2 text-left overflow-hidden truncate text-black">
          {{ privateKeyFile || 'Choose File' }}
        </button>
      </div>

      <div class="flex justify-between items-center">
        <label class="text-sm">Output Directory:</label>
        <button 
          @click="pickOutputDir" 
          class="bg-gray-200 px-4 py-2 rounded-md w-full sm:w-1/2 text-left overflow-hidden truncate text-black">
          {{ outputDir || 'Choose Folder' }}
        </button>
      </div>
    </div>

    <div class="flex justify-center">
      <button 
        @click="decrypt" 
        class="bg-blue-500 text-white px-6 py-3 rounded-md w-full sm:w-1/3 max-w-xs mt-6">
        Decrypt
      </button>
    </div>

    <p v-if="message" class="mt-4 text-green-600">{{ message }}</p>
    <p v-if="error" class="mt-4 text-red-600">{{ error }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { DecryptHybridZip, SelectPrivateKeyFile, SelectEncryptedFile, SelectOutputDirectory} from '../../wailsjs/go/backend/Decryptor'

const encryptedFile = ref(null)
const privateKeyFile = ref(null)
const outputDir = ref('')
const message = ref('')
const error = ref('')

const pickPrivateKeyFile = async () => {
  const result = await SelectPrivateKeyFile()
  if (result) privateKeyFile.value = result
}

const pickEncryptedFile = async () => {
  const result = await SelectEncryptedFile()
  if (result) encryptedFile.value = result
  console.log(encryptedFile.value)
}

const pickOutputDir = async () => {
  const result = await SelectOutputDirectory()
  if (result) outputDir.value = result
}

const decrypt = async () => {
  message.value = ''
  error.value = ''
  try {
    console.log(encryptedFile)
    if (!encryptedFile.value || !privateKeyFile.value || !outputDir.value) {
      error.value = 'Please select all required files and folder.'
      return
    }

    await DecryptHybridZip(
      encryptedFile.value,
      privateKeyFile.value,
      outputDir.value
    )
    message.value = 'Decryption complete!'
  } catch (err) {
    error.value = 'Decryption failed: ' + err
  }
}
</script>
