<template>
  <div class="module">
    <h2 class="module__meta-title" id="meta-title">{{module.metaTitle}}</h2>
    <h1 class="module__title" data-cy="module-title">{{module.title}}</h1>
    <img
      :src="module.heroImage"
      alt="" class="module__hero">
    <div class="module__intro" v-html="module.intro"></div>

    <h3 id="objectives">Lernziele</h3>

    <objective-groups :groups="languageCommunicationObjectiveGroups"></objective-groups>
    <add-objective-group-button v-if="!isStudent" type="languageCommunication"
                                :module="module.id"></add-objective-group-button>

    <objective-groups :groups="societyObjectiveGroups"></objective-groups>
    <add-objective-group-button v-if="!isStudent" type="society" :module="module.id"></add-objective-group-button>

    <chapter :chapter="chapter" :index="index" v-for="(chapter, index) in module.chapters" :key="chapter.id"></chapter>

  </div>
</template>

<script>
  import ObjectiveGroups from '@/components/objective-groups/ObjectiveGroups.vue';
  import ObjectiveGroupControl from '@/components/objective-groups/ObjectiveGroupControl.vue';
  import AddObjectiveGroupButton from '@/components/AddObjectiveGroupButton';
  import Chapter from '@/components/Chapter.vue';

  import UPDATE_OBJECTIVE_PROGRESS_MUTATION from '@/graphql/gql/mutations/updateObjectiveProgress.gql';
  import UPDATE_LAST_MODULE_MUTATION from '@/graphql/gql/mutations/updateLastModule.gql';
  import OBJECTIVE_QUERY from '@/graphql/gql/objectiveQuery.gql';
  import ME_QUERY from '@/graphql/gql/meQuery.gql';

  import {withoutOwnerFirst} from '@/helpers/sorting';

  export default {
    components: {
      ObjectiveGroups,
      ObjectiveGroupControl,
      AddObjectiveGroupButton,
      Chapter
    },

    props: {
      module: {
        required: true,
        type: Object
      },
      edit: {
        type: Boolean,
        default: false
      }
    },

    created() {
      this.updateLastVisitedModule(this.module.id)
    },

    computed: {
      languageCommunicationObjectiveGroups() {
        return this.module.objectiveGroups ? this.module.objectiveGroups
          .filter(group => group.title === 'LANGUAGE_COMMUNICATION')
          .sort(withoutOwnerFirst) : [];
      },
      societyObjectiveGroups() {
        return this.module.objectiveGroups ? this.module.objectiveGroups
          .filter(group => group.title === 'SOCIETY')
          .sort(withoutOwnerFirst) : [];
      },
      isStudent() {
        return !this.me.permissions.includes('users.can_manage_school_class_content');
      }
    },

    methods: {
      updateLastVisitedModule(moduleId) {
        this.$apollo.mutate({
          mutation: UPDATE_LAST_MODULE_MUTATION,
          variables: {
            input: {
              id: moduleId
            }
          },
          update(store, {data: {updateLastModule: {module}}}) {
            if (module) {
              const data = store.readQuery({query: ME_QUERY});
              if (data) {
                data.me.lastModule = module;
                store.writeQuery({query: ME_QUERY, data});
              }
            }
          }
        });
      },
      updateObjectiveProgress(done, objectiveId) {
        this.$apollo.mutate({
          mutation: UPDATE_OBJECTIVE_PROGRESS_MUTATION,
          variables: {
            input: {
              id: objectiveId,
              done: done
            }
          },
          update(store, {data: {updateObjectiveProgress: {objective}}}) {
            if (objective) {
              const variables = {id: objectiveId};
              const query = OBJECTIVE_QUERY;
              const data = store.readQuery({query, variables});
              if (data && data.objective.objectiveProgress.edges.length > 0) {
                data.objective.objectiveProgress.edges[0].node.done = done;
              }
              store.writeQuery({query: OBJECTIVE_QUERY, data, variables});
            }
          }
        })
      },
    },

    apollo: {
      me: {
        query: ME_QUERY,
      }
    },

    data() {
      return {
        me: {
          permissions: []
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "@/styles/_variables.scss";
  @import "@/styles/_functions.scss";
  @import "@/styles/_mixins.scss";

  .module {
    display: flex;
    justify-self: center;
    @include desktop {
      width: 800px;
    }
    flex-direction: column;
    padding: $large-spacing 15px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;

    &__hero {
      margin-bottom: 35px;
      border-radius: 12px;
      max-width: 100%;
    }

    &__meta-title {
      color: $color-silver-dark;
      font-size: 2.25rem;
      font-weight: 300;
      font-family: $serif-font-family;
      line-height: 3.25rem;
    }

    &__intro {
      line-height: 1.5;
      margin-bottom: 3em;

      > /deep/ p {
        font-size: toRem(25px);
        margin-bottom: 1.5em;

        &:last-child {
          margin-bottom: 0;
        }
      }
    }

  }
</style>
