<script>
/* eslint-disable vue/no-v-html */
import {
  GlAlert,
  GlBadge,
  GlDropdown,
  GlDropdownItem,
  GlEmptyState,
  GlIcon,
  GlLoadingIcon,
} from '@gitlab/ui';
import BurnCharts from 'ee/burndown_chart/components/burn_charts.vue';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import { formatDate } from '~/lib/utils/datetime_utility';
import { __ } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { Namespace } from '../constants';
import query from '../queries/iteration.query.graphql';
import IterationForm from './iteration_form.vue';
import IterationReportTabs from './iteration_report_tabs.vue';

const iterationStates = {
  closed: 'closed',
  upcoming: 'upcoming',
  expired: 'expired',
};

const page = {
  view: 'viewIteration',
  edit: 'editIteration',
};

export default {
  components: {
    BurnCharts,
    GlAlert,
    GlBadge,
    GlIcon,
    GlDropdown,
    GlDropdownItem,
    GlEmptyState,
    GlLoadingIcon,
    IterationForm,
    IterationReportTabs,
  },
  apollo: {
    iteration: {
      query,
      /* eslint-disable @gitlab/require-i18n-strings */
      variables() {
        return {
          fullPath: this.fullPath,
          id: convertToGraphQLId('Iteration', this.iterationId),
          isGroup: this.namespaceType === Namespace.Group,
        };
      },
      /* eslint-enable @gitlab/require-i18n-strings */
      update(data) {
        return data[this.namespaceType]?.iterations?.nodes[0] || {};
      },
      error(err) {
        this.error = err.message;
      },
    },
  },
  mixins: [glFeatureFlagsMixin()],
  props: {
    fullPath: {
      type: String,
      required: true,
    },
    hasScopedLabelsFeature: {
      type: Boolean,
      required: false,
      default: false,
    },
    iterationId: {
      type: String,
      required: false,
      default: undefined,
    },
    canEdit: {
      type: Boolean,
      required: false,
      default: false,
    },
    initiallyEditing: {
      type: Boolean,
      required: false,
      default: false,
    },
    labelsFetchPath: {
      type: String,
      required: false,
      default: '',
    },
    namespaceType: {
      type: String,
      required: false,
      default: Namespace.Group,
      validator: (value) => Object.values(Namespace).includes(value),
    },
    previewMarkdownPath: {
      type: String,
      required: false,
      default: '',
    },
    svgPath: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      isEditing: this.initiallyEditing,
      error: '',
      iteration: {},
    };
  },
  computed: {
    canEditIteration() {
      return this.canEdit && this.namespaceType === Namespace.Group;
    },
    loading() {
      return this.$apollo.queries.iteration.loading;
    },
    showEmptyState() {
      return !this.loading && this.iteration && !this.iteration.title;
    },
    status() {
      switch (this.iteration.state) {
        case iterationStates.closed:
          return {
            text: __('Closed'),
            variant: 'danger',
          };
        case iterationStates.expired:
          return { text: __('Past due'), variant: 'warning' };
        case iterationStates.upcoming:
          return { text: __('Upcoming'), variant: 'neutral' };
        default:
          return { text: __('Open'), variant: 'success' };
      }
    },
  },
  mounted() {
    this.boundOnPopState = this.onPopState.bind(this);
    window.addEventListener('popstate', this.boundOnPopState);
  },
  beforeDestroy() {
    window.removeEventListener('popstate', this.boundOnPopState);
  },
  methods: {
    onPopState(e) {
      if (e.state?.prev === page.view) {
        this.isEditing = true;
      } else if (e.state?.prev === page.edit) {
        this.isEditing = false;
      } else {
        this.isEditing = this.initiallyEditing;
      }
    },
    formatDate(date) {
      return formatDate(date, 'mmm d, yyyy', true);
    },
    loadEditPage() {
      this.isEditing = true;
      const newUrl = window.location.pathname.replace(/(\/edit)?\/?$/, '/edit');
      window.history.pushState({ prev: page.view }, null, newUrl);
    },
    loadReportPage() {
      this.isEditing = false;
      const newUrl = window.location.pathname.replace(/\/edit$/, '');
      window.history.pushState({ prev: page.edit }, null, newUrl);
    },
  },
};
</script>

<template>
  <div>
    <gl-alert v-if="error" variant="danger" @dismiss="error = ''">
      {{ error }}
    </gl-alert>
    <gl-loading-icon v-else-if="loading" class="gl-py-5" size="lg" />
    <gl-empty-state
      v-else-if="showEmptyState"
      :title="__('Could not find iteration')"
      :compact="false"
    />
    <iteration-form
      v-else-if="isEditing"
      :group-path="fullPath"
      :preview-markdown-path="previewMarkdownPath"
      :is-editing="true"
      :iteration="iteration"
      @updated="loadReportPage"
      @cancel="loadReportPage"
    />
    <template v-else>
      <div
        ref="topbar"
        class="gl-display-flex gl-justify-items-center gl-align-items-center gl-py-3 gl-border-1 gl-border-b-solid gl-border-gray-100"
      >
        <gl-badge :variant="status.variant">
          {{ status.text }}
        </gl-badge>
        <span class="gl-ml-4"
          >{{ formatDate(iteration.startDate) }} – {{ formatDate(iteration.dueDate) }}</span
        >
        <gl-dropdown
          v-if="canEditIteration"
          data-testid="actions-dropdown"
          variant="default"
          toggle-class="gl-text-decoration-none gl-border-0! gl-shadow-none!"
          class="gl-ml-auto gl-text-secondary"
          right
          no-caret
        >
          <template #button-content>
            <gl-icon name="ellipsis_v" /><span class="gl-sr-only">{{ __('Actions') }}</span>
          </template>
          <gl-dropdown-item @click="loadEditPage">{{ __('Edit iteration') }}</gl-dropdown-item>
        </gl-dropdown>
      </div>
      <h3 ref="title" class="page-title">{{ iteration.title }}</h3>
      <div ref="description" v-html="iteration.descriptionHtml"></div>
      <burn-charts
        :start-date="iteration.startDate"
        :due-date="iteration.dueDate"
        :iteration-id="iteration.id"
        :iteration-state="iteration.state"
        :full-path="fullPath"
        :namespace-type="namespaceType"
      />
      <iteration-report-tabs
        :full-path="fullPath"
        :has-scoped-labels-feature="hasScopedLabelsFeature"
        :iteration-id="iteration.id"
        :labels-fetch-path="labelsFetchPath"
        :namespace-type="namespaceType"
        :svg-path="svgPath"
      />
    </template>
  </div>
</template>
