<template>
  <div id="config">
    <section v-if="['GMSJ01', 'GMSJ0A'].includes(version)" class="appearance">
      <h3>{{l.h3}}</h3>
      <div>
        <span>{{l.location}}(</span><input type="number" min="0" max="600" v-model.number="x"><span>, </span><input type="number" min="16" max="464" v-model.number="y"><span>)</span>
      </div>
      <div>
        <span>{{l.fontSize}}</span><input type="number" min="0" v-model.number="fontSize">
      </div>
      <div>
        <span>{{fgRGB2==null ? l.fgColor : l.fgColor1}}</span><input type="color" :value="rgbI2S(fgRGB)" @change="fgRGB = rgbS2I($event.target.value)">
        <span>{{l.alpha}}</span><input type="number" min="0" max="255" v-model.number="fgA"><span>/255={{(fgA/2.55).toFixed(1)}}%</span>
        <input type="checkbox" :checked="fgRGB2!=null" @change="toggleGradient"><span>{{l.fgColorGrad}}</span>
      </div>
      <div v-if="fgRGB2 != null">
        <span>{{l.fgColor2}}</span><input type="color" :value="rgbI2S(fgRGB2)" @change="fgRGB2 = rgbS2I($event.target.value)">
        <span>{{l.alpha}}</span><input type="number" min="0" max="255" v-model.number="fgA2"><span>/255={{(fgA2/2.55).toFixed(1)}}%</span>
      </div>
      <div>
        <span>{{l.bgColor}}</span><input type="color" :value="rgbI2S(bgRGB)" @change="bgRGB = rgbS2I($event.target.value)">
        <span>{{l.alpha}}</span><input type="number" min="0" max="255" v-model.number="bgA"><span>/255={{(bgA/2.55).toFixed(1)}}%</span>
      </div>
      <h4>{{l.preview}}</h4>
      <Preview :config="codeConfigs" />
      <div style="white-space: pre">{{l.previewNote}}</div>
    </section>
    <section class="freeze">
      <h3>{{l.freeze.h3}}</h3>
      <div>
        {{l.freeze.duration}}<input type="number" min="0" max="32767" v-model="freezeDuration"> {{l.freeze.frame}}
        = {{(freezeDuration*1001/30000).toFixed(2)}} {{l.freeze.sec}}
      </div>
      <table>
        <tbody>
          <tr v-for="key in freezeKeys" :key="key">
            <td>{{l.freeze.rows[key]}}</td>
            <td><input type="checkbox" :checked="freeze[key]" @change="onChangeFreeze($event, key)"></td>
          </tr>
        </tbody>
      </table>
    </section>
  </div>
</template>

<script>
// import Preview from '../../PreviewString.vue';
import {getConfig, lskey, codes} from './codegen.js';
import labels from './labels.json';

function updateConfig() {
  const {x, y, fontSize, width, fgRGB, fgA, fgRGB2, fgA2, bgRGB, bgA, freeze, freezeDuration} = this;
  const config = {
    x, y, fontSize, width, fgRGB, fgA, fgRGB2, fgA2, bgRGB, bgA, freeze, freezeDuration
  };
  localStorage.setItem(lskey, JSON.stringify(config));
  this.$emit('config', config);
}

export default {
  props: {
    version: {type: String},
    codeConfigs: {type: Object},
  },
  methods: {
    onChangeFreeze($event, key) {
      this.freeze[key] = $event.target.checked;
      this.updateConfig();
    },
    toggleGradient($event) {
      if ($event.target.checked) {
        this.fgRGB2 = this.fgRGB;
        this.fgA2 = this.fgA;
      } else {
        this.fgRGB2 = null;
        this.fgA2 = null;
      }
    },
    rgbI2S: (x) => '#'+x.toString(16).padStart(6, '0'),
    rgbS2I: (s) => parseInt(s.slice(1), 16),
    rgbaI2S: (rgb, a) => '#'+rgb.toString(16).padStart(6, '0')+a.toString(16).padStart(2, '0'),
  },
  data() {
    const {x, y, fontSize, width, fgRGB, fgA, fgRGB2, fgA2, bgRGB, bgA, freeze, freezeDuration} = getConfig();
    return {
      x, y, fontSize, width,
      fgRGB, fgA, fgRGB2, fgA2, bgRGB, bgA,
      freeze, freezeDuration,
      // const
      freezeKeys: Object.keys(codes[this.version]?.freezeCodeInfo ?? {}),
    };
  },
  computed: {
    l() {
      return labels[this.$lang] ?? labels['en-US'];
    },
  },
  watch: {
    x: updateConfig,
    y: updateConfig,
    fontSize: updateConfig,
    width: updateConfig,
    fgRGB: updateConfig,
    fgA: updateConfig,
    fgRGB2: updateConfig,
    fgA2: updateConfig,
    bgRGB: updateConfig,
    bgA: updateConfig,
    freezeDuration: updateConfig,
  },
}
</script>

<style scoped>
input[type=number], td.right {
  text-align: right;
}
input[type="number"] {
  width: 3em;
  margin: 0 2px;
}
.appearance > div {
  padding: 0 0 4px;
}

input[type=number] {
  -moz-appearance: textfield;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
</style>
