<template>
  <div class="submission-page">
    <div class="submission-page__header submission-header">
      <h2>Aufgabe</h2>
      <p>{{ studentSubmission.assignment.assignment }}</p>
    </div>
    <div class="submission-page__content submission-content">
      <h2>Ergebnis von {{ fullName }}</h2>
      <p v-html="text"/>
      <p
        class="article-content__document"
        v-if="studentSubmission.document && studentSubmission.document.length > 0">
        <a
          :href="studentSubmission.document"
          class="entry-document__link link"
          target="_blank">
          <student-submission-document :document="studentSubmission.document"/>
        </a>
      </p>
    </div>
    <div class="submission-page__feedback feedback">
      <submission-form
        :user-input="feedback"
        :saved="!unsaved"
        :read-only="readOnly"
        placeholder="Feedback erfassen"
        action="Feedback teilen"
        shared-msg="Dieses Feedback wurde geteilt."
        v-if="studentSubmission"
        @turnIn="turnIn"
        @saveInput="saveInput"
        @reopen="reopen"
      >
        <emoji-bar
          class="feedback-submission__emojis emojis"
          v-if="!final"
          @add-emoji="addEmoji" />

      </submission-form>
    </div>
  </div>
</template>

<script>
  import {newLineToParagraph} from '@/helpers/text';
  import debounce from 'lodash/debounce';
  import cloneDeep from 'lodash/cloneDeep';

  import StudentSubmissionDocument from '@/components/StudentSubmissionDocument';
  import STUDENT_SUBMISSIONS_QUERY from '@/graphql/gql/queries/studentSubmissionQuery.gql';
  import UPDATE_FEEDBACK_MUTATION from '@/graphql/gql/mutations/updateFeedback.gql';
  import UPDATE_FEEDBACK_WITH_TEXT_MUTATION from '@/graphql/gql/mutations/updateFeedbackWithText.gql';
  import SubmissionForm from '@/components/content-blocks/assignment/SubmissionForm';

  import me from '@/mixins/me';
  import EmojiBar from '@/components/ui/EmojiBar';

  export default {

    mixins: [me],
    components: {
      EmojiBar,
      StudentSubmissionDocument,
      SubmissionForm,
    },

    data() {
      return {
        studentSubmission: {
          assignment: {
            title: '',
          },
          student: {
            firstName: '',
            lastName: '',
          },
          text: '',
          document: '',
          submissionFeedback: {
            text: '',
            final: false,
          },
        },
        unsaved: false,
        saving: 0,
      };
    },

    computed: {
      final() {
        return !!this.studentSubmission && this.studentSubmission.final;
      },
      text() {
        return newLineToParagraph(this.studentSubmission.text);
      },
      fullName() {
        return `${this.studentSubmission.student.firstName} ${this.studentSubmission.student.lastName}`;
      },
      feedback() {
        return this.studentSubmission.submissionFeedback ? this.studentSubmission.submissionFeedback : {
          text: '',
          final: false,
        };
      },
      readOnly() {
        return this.me.readOnly || this.me.selectedClass.readOnly;
      }
    },

    apollo: {
      studentSubmission() {
        return {
          query: STUDENT_SUBMISSIONS_QUERY,
          variables() {
            return {
              id: this.$route.params.id,
            };
          },
          result({data: {studentSubmission}}) {
            if (!studentSubmission.submissionFeedback) { // create something for the cache to find
              this.create();
            }
            this.studentSubmission = cloneDeep(studentSubmission); // we don't want to update the value when the server updates
          },
        };
      },
    },

    methods: {
      addEmoji(emoji) {
        const feedbackText = this.feedback.text + emoji;
        this.updateFeedbackText(feedbackText);
      },
      _save: debounce(function () {
        this.saving++;
        this.$apollo.mutate({
          mutation: UPDATE_FEEDBACK_MUTATION,
          variables: {
            input: {
              submissionFeedback: {
                studentSubmission: this.studentSubmission.id,
                text: this.studentSubmission.submissionFeedback.text,
              },
            },
          },
          update: this.updateCache,
        }).then(() => {
          this.saving--;
          if (this.saving === 0) {
            this.unsaved = false;
          }
        });
      }, 500),
      saveInput: function (feedbackText) {
        this.unsaved = true;
        /*
          We update the assignment on this component, so the changes are reflected on it. The server does not return
          the updated entity, to prevent the UI to update when the user is entering his input
         */
        this.updateFeedbackText(feedbackText);
        this._save();
      },
      update({withText, text, final}) {
        if (this.readOnly) {
          this.$log.debug('read-only');
          return;
        }
        let mutation = withText ? UPDATE_FEEDBACK_WITH_TEXT_MUTATION : UPDATE_FEEDBACK_MUTATION;
        this.$apollo.mutate({
          mutation,
          variables: {
            input: {
              submissionFeedback: {
                studentSubmission: this.studentSubmission.id,
                text: text,
                final: final,
              },
            },
          },
          update: this.updateCache,
        });
      },
      create() {
        this.$log.debug('create');
        this.update({
          withText: true,
          text: '',
          final: false,
        });
      },
      turnIn() {
        this.$log.debug('turnIn');
        this.update({
          withText: true,
          text: this.studentSubmission.submissionFeedback.text,
          final: true,
        });
      },
      reopen() {
        this.$log.debug('reopen');
        if (!this.studentSubmission.id) {
          return;
        }

        this.update({
          withText: false,
          text: this.studentSubmission.submissionFeedback.text,
          final: false,
        });
      },
      updateCache(store, {data: {updateSubmissionFeedback: {successful, updatedSubmissionFeedback}}}) {
        try {
          if (successful) {
            const query = STUDENT_SUBMISSIONS_QUERY;
            const variables = {
              id: this.studentSubmission.id,
            };
            const data = store.readQuery({query, variables});

            if (data) {
              if (!data.studentSubmission.submissionFeedback) {
                data.studentSubmission.submissionFeedback = {
                  '__typename': 'SubmissionFeedbackNode',
                };
              }

              data.studentSubmission.submissionFeedback = Object.assign({}, data.studentSubmission.submissionFeedback, {
                id: updatedSubmissionFeedback.id,
                final: updatedSubmissionFeedback.final,
              });

              if (updatedSubmissionFeedback.text !== undefined) {
                // text is only being set on create and on turning in, then we'll update the cache with it. Otherwise, we'll trust the local state, as to not overwrite the input field
                data.studentSubmission.submissionFeedback.text = updatedSubmissionFeedback.text;
              } else {
                data.studentSubmission.submissionFeedback.text = this.studentSubmission.submissionFeedback ? this.studentSubmission.submissionFeedback.text : '';
              }

              store.writeQuery({query, variables, data});
            }
          }
        } catch (e) {
          console.error(e);
          // Query did not exist in the cache, and apollo throws a generic Error. Do nothing
        }
      },
      updateFeedbackText(text) {
        this.studentSubmission = Object.assign({}, this.studentSubmission, {
          submissionFeedback: Object.assign({}, this.studentSubmission.submissionFeedback, {text: text}),
        });
      },
    },
  };
</script>

<style scoped lang="scss">
  @import "~styles/helpers";

  .submission-page {
    &__content {
      margin-top: 1.5*$large-spacing;
    }

    &__feedback {
      margin-top: $large-spacing;
    }
  }

  .emojis {
    font-size: toRem(32px);

    &__emoji {
      cursor: pointer;
    }
  }
</style>
