<template>
  <q-card class="col-12" height="">
    <q-card-section v-if="isThisSession(session.token)" class="text-caption">
      Diese Session.
    </q-card-section>
    <q-card-section>
      <div class="row">
        <div class="col-xs-12 col-sm-6">
          Browser:
          <q-icon :name="getBrowserIcon(session.browser)" size="24px" />
          {{ session.browser }}
        </div>
        <div class="col-xs-12 col-sm-6">
          Plattform:
          <q-icon :name="getPlatformIcon(session.platform)" size="24px" />
          {{ session.platform }}
        </div>
      </div>
      <div class="row" v-if="!isEdit">
        <div class="col-xs-12 col-sm-6">
          Lebenszeit:
          {{ session.lifetime }}
        </div>
        <div class="col-xs-12 col-sm-6">Läuft aus: {{ session.expires | dateTime(true) }}</div>
      </div>
      <div class="row q-my-sm" v-else>
        <q-input
          class="col-xs-12 col-sm-6 q-px-sm"
          v-model="computedLifetime"
          type="number"
          label="Zeit"
          filled
        />
        <q-select
          class="col-xs-12 col-sm-6 q-px-sm"
          :options="options"
          v-model="option"
          filled
        />
      </div>
    </q-card-section>
    <q-card-actions align="right" v-if="!isEdit">
      <q-btn flat round dense icon="mdi-pencil" @click="edit(true)" />
      <q-btn flat round dense icon="mdi-delete" @click="deleteSession(session.token)" />
    </q-card-actions>
    <q-card-actions align="right" v-else>
      <q-btn flat dense label="Abbrechen" @click="edit(false)" />
      <q-btn flat dense label="Speichern" @click="save" />
    </q-card-actions>
  </q-card>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from '@vue/composition-api';
import { Store } from 'vuex';
import { StateInterface } from 'src/store';

export default defineComponent({
  name: 'Sessions',
  props: {
    session: {
      required: true,
    },
  },
  setup(props: {session: FG.Session}, { root }) {
    const store = <Store<StateInterface>>root.$store;
    const options = ref(['Minuten', 'Stunden', 'Tage']);
    const option = ref<string>(options.value[0]);
    const lifetime = ref(0);
    function getBrowserIcon(browser: string) {
      return browser == 'firefox'
        ? 'mdi-firefox'
        : browser == 'chrome'
        ? 'mdi-google-chrome'
        : browser == 'safari'
        ? 'mdi-apple-safari'
        : 'mdi-help';
    }

    function getPlatformIcon(platform: string) {
      return platform == 'linux'
        ? 'mdi-linux'
        : platform == 'windows'
        ? 'mdi-microsoft-windows'
        : platform == 'macos'
        ? 'mdi-apple'
        : platform == 'iphone'
        ? 'mdi-cellphone-iphone'
        : platform == 'android'
        ? 'mdi-cellphone-android'
        : 'mdi-help';
    }

    function deleteSession(token: string) {
      store.dispatch('session/deleteSession', token).catch((error) => {
        console.warn(error);
      });
    }
    function isThisSession(token: string) {
      return store.state.session.currentSession?.token === token;
    }

    const isEdit = ref(false);

    const computedLifetime = computed({
      get: () => {
        switch (option.value) {
          case options.value[0]:
            return (lifetime.value / 60).toFixed(2);
          case options.value[1]:
            return (lifetime.value / (60 * 60)).toFixed(2);
          case options.value[2]:
            return (lifetime.value / (60 * 60 * 24)).toFixed(2);
        }
      },
      set: (val) => {
        if (val) {
          switch (option.value) {
            case options.value[0]:
              lifetime.value = parseFloat(val) * 60;
              break;
            case options.value[1]:
              lifetime.value = parseFloat(val) * 60 * 60;
              break;
            case options.value[2]:
              lifetime.value = parseFloat(val) * 60 * 60 * 24;
              break;
          }
        }
      },
    });

    function edit(value: boolean) {
      lifetime.value = props.session.lifetime;
      isEdit.value = value;
    }

    function save() {
      console.log(lifetime.value);
      isEdit.value = false;
      void store
        .dispatch(
          'session/updateSession',
          {lifetime: lifetime.value, token: props.session.token}
        )
        .catch((error) => {
          console.log(error);
        });
    }

    return {
      getBrowserIcon,
      getPlatformIcon,
      isThisSession,
      deleteSession,
      isEdit,
      edit,
      options,
      option,
      lifetime,
      computedLifetime,
      save,
    };
  },
});
</script>
