<template>
  <div class="scenery-timetables-history">
    <div class="history-modes">
      <button
        class="btn btn--option"
        v-for="mode in historyModeList"
        :key="mode"
        :class="{ checked: checkedHistoryMode == mode }"
        @click="checkHistoryMode(mode)"
      >
        {{ $t(`scenery.timetable-${mode}`) }}
      </button>
    </div>

    <div class="history-wrapper">
      <Loading v-if="dataStatus != DataStatus.Loaded" />

      <div v-else-if="historyList.length == 0" class="no-history">
        {{ $t('scenery.history-list-empty') }}
      </div>

      <div v-else class="journal-list">
        <div v-for="timetableHistory in historyList" :key="timetableHistory.id">
          <span>
            <div>
              <span
                class="timetable-status-indicator"
                :data-terminated="timetableHistory.terminated"
                :data-fulfilled="timetableHistory.fulfilled"
              >
                &ofcir;
              </span>
              #{{ timetableHistory.id }} |
              <b class="text--primary">{{ timetableHistory.trainCategoryCode }}</b>
              {{ timetableHistory.trainNo }}
              {{ timetableHistory.route.replace('|', ' &Rightarrow; ') }}
            </div>

            <div class="text--grayed">
              <span>
                {{ $t('scenery.timetable-issued-date') }}
                <b>
                  {{ parseCreatedDate(timetableHistory, $i18n.locale) }}
                </b>
              </span>

              <span>
                {{ $t('scenery.timetable-issued-for') }}
                <router-link
                  class="journal-link"
                  :to="`/journal/timetables?search-driver=${timetableHistory.driverName}`"
                >
                  {{ timetableHistory.driverName }}
                </router-link>
              </span>

              <span v-if="timetableHistory.authorName">
                {{ $t('scenery.timetable-issued-by') }}
                <router-link
                  class="journal-link"
                  :to="`/journal/timetables?search-dispatcher=${timetableHistory.authorName}`"
                >
                  {{ timetableHistory.authorName }}
                </router-link>
              </span>
            </div>
          </span>

          <button
            @click="
              navigateTo(`/journal/timetables`, {
                'search-train': `#${timetableHistory.id}`
              })
            "
          >
            <img src="/images/icon-back.svg" alt="icon navigate to timetable" />
          </button>
        </div>
      </div>
    </div>

    <div class="bottom-info">
      <button class="btn btn--option" v-if="historyList.length > 0" @click="navigateToHistory()">
        {{ $t('scenery.bottom-info') }}
      </button>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue';
import dateMixin from '../../mixins/dateMixin';

import Loading from '../Global/Loading.vue';
import { API } from '../../typings/api';
import { ActiveScenery, Station, Status } from '../../typings/common';
import { useApiStore } from '../../store/apiStore';
import routerMixin from '../../mixins/routerMixin';
import { useMainStore } from '../../store/mainStore';

const historyModeList = ['includesScenery', 'issuedFrom', 'via', 'terminatingAt'] as const;
type HistoryMode = (typeof historyModeList)[number];

export default defineComponent({
  name: 'SceneryTimetablesHistory',
  mixins: [dateMixin, routerMixin],
  props: {
    station: {
      type: Object as PropType<Station>
    },
    onlineScenery: {
      type: Object as PropType<ActiveScenery>
    }
  },

  data() {
    return {
      historyList: [] as API.TimetableHistory.ResponseShort,
      historyModeList,

      apiStore: useApiStore(),
      mainStore: useMainStore(),
      dataStatus: Status.Data.Loading,
      DataStatus: Status.Data,

      checkedHistoryMode: 'includesScenery' as HistoryMode
    };
  },

  async activated() {
    this.checkedHistoryMode = 'includesScenery';
    this.fetchAPIData();
  },

  methods: {
    async fetchAPIData() {
      const stationName = this.$route.query['station'];
      this.dataStatus = Status.Data.Loading;

      if (!stationName) {
        this.historyList = [];
        this.dataStatus = Status.Data.Loaded;
        return;
      }

      const requestFilters: Record<string, any> = {};
      requestFilters[this.checkedHistoryMode] = stationName.toString();
      requestFilters.countLimit = 30;
      requestFilters['returnType'] = 'short';

      try {
        const response: API.TimetableHistory.ResponseShort = await this.apiStore.client.get(
          'api/getTimetables',
          requestFilters
        );

        console.log(response);

        this.historyList = response;

        this.dataStatus = Status.Data.Loaded;
      } catch (error) {
        console.error(error);
        this.dataStatus = Status.Data.Error;
      }
    },

    checkHistoryMode(mode: HistoryMode) {
      this.checkedHistoryMode = mode;
      this.fetchAPIData();
    },

    navigateToHistory() {
      this.$router.push({
        path: '/journal/timetables',
        query: {
          [`search-${this.checkedHistoryMode}`]: this.station?.name || this.onlineScenery?.name
        }
      });
    },

    parseCreatedDate(timetable: API.TimetableHistory.DataShort, locale: string) {
      const createdDate =
        timetable.createdAt > timetable.beginDate
          ? new Date(timetable.beginDate)
          : new Date(timetable.createdAt);

      return createdDate.toLocaleString(locale == 'pl' ? 'pl-PL' : 'en-GB', {
        timeStyle: 'short',
        dateStyle: 'medium'
      });
    }
  },
  components: { Loading }
});
</script>

<style lang="scss" scoped>
@use '../../styles/responsive';
@use '../../styles/scenery-history-table';

.scenery-timetables-history {
  height: 100%;
  overflow: auto;

  display: grid;
  gap: 1em;
  grid-template-rows: auto 1fr 40px;
}

.history-wrapper {
  position: relative;
  overflow: auto;
}

.history-modes {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;

  gap: 0.5em;
  padding: 0.25em;

  button {
    padding: 0.35em;
  }
}

.journal-link {
  font-weight: bold;
  color: #eee;

  &:hover {
    color: var(--clr-primary);
  }
}

.journal-list {
  display: flex;
  flex-direction: column;
  gap: 0.5em;
  text-align: left;
}

.journal-list > div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5em;
  background-color: #2b2b2b;
  line-height: 1.5em;
}

.journal-list > div > button > img {
  width: 2em;
  transform: rotate(180deg);
}

.timetable-status-indicator {
  &[data-fulfilled='true'] {
    color: lightgreen;
  }

  &[data-terminated='false'] {
    color: lightblue;
  }

  &[data-terminated='true'][data-fulfilled='false'] {
    color: lightcoral;
  }
}
</style>
