<script setup lang="ts">
import figlet from 'figlet';
import TextareaCopyable from '@/components/TextareaCopyable.vue';

const input = ref('Ascii ART');
const font = useStorage('ascii-text-drawer:font', 'Standard');
const width = useStorage('ascii-text-drawer:width', 80);
const output = ref('');
const errored = ref(false);
const processing = ref(false);

figlet.defaults({ fontPath: '/figlet' });

watchEffect(async () => {
  processing.value = true;
  try {
    const options: figlet.Options = {
      font: font.value as figlet.Fonts,
      width: width.value,
      whitespaceBreak: true,
    };
    output.value = await (new Promise<string>((resolve, reject) =>
      figlet.text(input.value, options,
        (err, text) => {
          if (err) {
            reject(err);
            return;
          }

          resolve(text ?? '');
        })));
    errored.value = false;
  }
  catch (e: any) {
    errored.value = true;
  }
  processing.value = false;
});

const fonts = ['1Row', '3-D', '3D Diagonal', '3d_diagonal', '3D-ASCII', '3d', '3x5', '4Max', '5 Line Oblique', '5lineoblique', 'Acrobatic', 'Alligator', 'Alligator2', 'alligator3', 'Alpha', 'Alphabet', 'AMC 3 Line', 'AMC 3 Liv1', 'AMC AAA01', 'AMC Neko', 'AMC Razor', 'AMC Razor2', 'AMC Slash', 'AMC Slider', 'AMC Thin', 'AMC Tubes', 'AMC Untitled', 'amc3line', 'amc3liv1', 'amcaaa01', 'amcneko', 'amcrazo2', 'amcrazor', 'amcslash', 'amcslder', 'amcthin', 'amctubes', 'amcun1', 'ANSI Regular', 'ANSI Shadow', 'Arrows', 'ASCII New Roman', 'ascii_new_roman', 'Avatar', 'B1FF', 'Banner', 'Banner3-D', 'Banner3', 'Banner4', 'Barbwire', 'Basic', 'Bear', 'Bell', 'Benjamin', 'Big Chief', 'Big Money-ne', 'Big Money-nw', 'Big Money-se', 'Big Money-sw', 'Big', 'bigchief', 'Bigfig', 'Binary', 'Block', 'Blocks', 'Bloody', 'Bolger', 'Braced', 'Bright', 'Broadway KB', 'broadway_kb', 'Broadway', 'Bubble', 'Bulbhead', 'calgphy2', 'Caligraphy', 'Caligraphy2', 'Calvin S', 'Cards', 'Catwalk', 'Chiseled', 'Chunky', 'Coinstak', 'Cola', 'Colossal', 'Computer', 'Contessa', 'Contrast', 'cosmic', 'Cosmike', 'Crawford', 'Crawford2', 'Crazy', 'Cricket', 'Cursive', 'Cyberlarge', 'Cybermedium', 'Cybersmall', 'Cygnet', 'DANC4', 'Dancing Font', 'dancingfont', 'Decimal', 'Def Leppard', 'defleppard', 'Delta Corps Priest 1', 'Diamond', 'Diet Cola', 'dietcola', 'Digital', 'Doh', 'Doom', 'DOS Rebel', 'dosrebel', 'Dot Matrix', 'dotmatrix', 'Double Shorts', 'Double', 'doubleshorts', 'Dr Pepper', 'drpepper', 'DWhistled', 'Efti Chess', 'Efti Font', 'Efti Italic', 'Efti Piti', 'Efti Robot', 'Efti Wall', 'Efti Water', 'eftichess', 'eftifont', 'eftipiti', 'eftirobot', 'eftitalic', 'eftiwall', 'eftiwater', 'Electronic', 'Elite', 'Epic', 'Fender', 'Filter', 'Fire Font-k', 'Fire Font-s', 'fire_font-k', 'fire_font-s', 'Flipped', 'Flower Power', 'flowerpower', 'Four Tops', 'fourtops', 'Fraktur', 'Fun Face', 'Fun Faces', 'funface', 'funfaces', 'Fuzzy', 'Georgi16', 'Georgia11', 'Ghost', 'Ghoulish', 'Glenyn', 'Goofy', 'Gothic', 'Graceful', 'Gradient', 'Graffiti', 'Greek', 'halfiwi', 'Heart Left', 'Heart Right', 'heart_left', 'heart_right', 'Henry 3D', 'henry3d', 'Hex', 'Hieroglyphs', 'Hollywood', 'Horizontal Left', 'Horizontal Right', 'horizontalleft', 'horizontalright', 'ICL-1900', 'Impossible', 'Invita', 'Isometric1', 'Isometric2', 'Isometric3', 'Isometric4', 'Italic', 'Ivrit', 'Jacky', 'Jazmine', 'Jerusalem', 'JS Block Letters', 'JS Bracket Letters', 'JS Capital Curves', 'JS Cursive', 'JS Stick Letters', 'Katakana', 'Kban', 'Keyboard', 'Knob', 'koholint', 'kompaktblk', 'Konto Slant', 'Konto', 'kontoslant', 'Larry 3D 2', 'Larry 3D', 'larry3d', 'LCD', 'Lean', 'Letters', 'Lil Devil', 'lildevil', 'Line Blocks', 'lineblocks', 'Linux', 'Lockergnome', 'Madrid', 'Marquee', 'Maxfour', 'maxiwi', 'Merlin1', 'Merlin2', 'Mike', 'Mini', 'miniwi', 'Mirror', 'Mnemonic', 'Modular', 'Morse', 'Morse2', 'Moscow', 'Mshebrew210', 'Muzzle', 'Nancyj-Fancy', 'Nancyj-Improved', 'Nancyj-Underlined', 'Nancyj', 'Nipples', 'NScript', 'NT Greek', 'ntgreek', 'NV Script', 'O8', 'Octal', 'Ogre', 'Old Banner', 'oldbanner', 'OS2', 'Patorjk-HeX', 'Patorjk\'s Cheese', 'Pawp', 'Peaks Slant', 'Peaks', 'peaksslant', 'Pebbles', 'Pepper', 'Poison', 'Puffy', 'Puzzle', 'Pyramid', 'Rammstein', 'Rectangles', 'Red Phoenix', 'red_phoenix', 'Relief', 'Relief2', 'rev', 'Reverse', 'Roman', 'Rot13', 'Rotated', 'Rounded', 'Rowan Cap', 'rowancap', 'Rozzo', 'Runic', 'Runyc', 'S Blood', 's-relief', 'Santa Clara', 'santaclara', 'sblood', 'Script', 'Serifcap', 'Shadow', 'Shimrod', 'Short', 'six-fo', 'SL Script', 'Slant Relief', 'Slant', 'Slide', 'slscript', 'Small Caps', 'Small Isometric1', 'Small Keyboard', 'Small Poison', 'Small Script', 'Small Shadow', 'Small Slant', 'Small Tengwar', 'Small', 'smallcaps', 'smisome1', 'smkeyboard', 'smpoison', 'smscript', 'smshadow', 'smslant', 'smtengwar', 'Soft', 'Speed', 'Spliff', 'Stacey', 'Stampate', 'Stampatello', 'Standard', 'Star Strips', 'Star Wars', 'starstrips', 'starwars', 'Stellar', 'stencil', 'Stforek', 'Stick Letters', 'Stop', 'Straight', 'Stronger Than All', 'Sub-Zero', 'Swamp Land', 'swampland', 'Swan', 'Sweet', 'Tanja', 'Tengwar', 'Term', 'Test1', 'The Edge', 'Thick', 'Thin', 'THIS', 'Thorned', 'Three Point', 'threepoint', 'Ticks Slant', 'Ticks', 'ticksslant', 'Tiles', 'Tinker-Toy', 'Tombstone', 'Train', 'Trek', 'Tsalagi', 'Tubes-Regular', 'Tubes-Smushed', 'Tubular', 'Twisted', 'Two Point', 'twopoint', 'ublk', 'Univers', 'USA Flag', 'usaflag', 'Varsity', 'Wavy', 'Weird', 'Wet Letter', 'wetletter', 'Whimsy', 'Wow'];
</script>

<template>
  <c-card style="max-width: 600px;">
    <c-input-text
      v-model:value="input"
      label="Your text:"
      placeholder="Your text to draw"
      raw-text
      multiline
      rows="4"
    />

    <n-divider />

    <n-grid cols="4" x-gap="12" w-full>
      <n-gi span="2">
        <c-select
          v-model:value="font"
          label-position="top"
          label="Font:"
          :options="fonts"
          searchable="true"
          placeholder="Select font to use"
        />
      </n-gi>
      <n-gi span="2">
        <n-form-item label="Width:" label-placement="top" label-width="100" :show-feedback="false">
          <n-input-number v-model:value="width" min="0" max="10000" w-full placeholder="Width of the text" />
        </n-form-item>
      </n-gi>
    </n-grid>

    <n-divider />

    <div v-if="processing" flex items-center justify-center>
      <n-spin size="medium" />
      <span class="ml-2">Loading font...</span>
    </div>

    <c-alert v-if="errored" mt-1 text-center type="error">
      Current settings resulted in error.
    </c-alert>

    <n-form-item v-if="!processing && !errored" label="Ascii Art text:">
      <TextareaCopyable
        :value="output"
        mb-1 mt-1
        copy-placement="outside"
      />
    </n-form-item>
  </c-card>
</template>
