<template>
  <div class="text-md-end">
    <span class="d-block d-sm-inline-block mb-md-2">
      <a @click="toggleIntensity" class="flag-toggle">
        <span :class="{ on: !isRegular }">{{ $t("course.occasional") }}</span>
        <i
          class="fa"
          :class="{
            'fa-toggle-on': isRegular,
            'fa-toggle-on fa-flip-horizontal': !isRegular,
          }"
        />
        <span :class="{ on: isRegular }">{{ $t("course.regular") }}</span>
      </a>
    </span>

    <span class="d-block d-sm-inline-block ms-sm-3 ms-md-0">
      <button
        class="badge rounded-pill me-1"
        :class="{
          'bg-danger': isEmergency,
          'bg-secondary': !isEmergency,
        }"
        @click="toggleEmergency"
      >
        {{ $t("course.emergency") }}
      </button>
      <button
        class="badge rounded-pill"
        :class="{
          'bg-danger': isConfidential,
          'bg-secondary': !isConfidential,
        }"
        @click="toggleConfidential"
      >
        {{ $t("course.confidential") }}
      </button>
    </span>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "ToggleFlags",
  computed: {
    ...mapState({
      intensity: (state) => state.accompanyingCourse.intensity,
      emergency: (state) => state.accompanyingCourse.emergency,
      confidential: (state) => state.accompanyingCourse.confidential,
      permissions: (state) => state.permissions,
    }),
    isRegular() {
      return this.intensity === "regular" ? true : false;
    },
    isEmergency() {
      return this.emergency ? true : false;
    },
    isConfidential() {
      return this.confidential ? true : false;
    },
  },
  methods: {
    toggleIntensity() {
      let value;
      switch (this.intensity) {
        case "occasional":
          value = "regular";
          break;
        case "regular":
          value = "occasional";
          break;
        default:
          //temporaire (modif backend)
          value = "occasional";
      }
      this.$store.dispatch("toggleIntensity", value).catch(({ name }) => {
        if (name === "ValidationException" || name === "AccessException") {
          this.$toast.open({
            message: this.$t(
              "Only the referrer can toggle the intensity of an accompanying course",
            ),
          });
        } else {
          this.$toast.open({ message: "An error occurred" });
        }
      });
    },
    toggleEmergency() {
      this.$store
        .dispatch("toggleEmergency", !this.isEmergency)
        .catch(({ name, violations }) => {
          if (name === "ValidationException" || name === "AccessException") {
            violations.forEach((violation) =>
              this.$toast.open({ message: violation }),
            );
          } else {
            this.$toast.open({ message: "An error occurred" });
          }
        });
    },
    toggleConfidential() {
      this.$store.dispatch("toggleConfidential").catch(({ name }) => {
        console.log(name);
        if (name === "ValidationException" || name === "AccessException") {
          this.$toast.open({
            message: this.$t(
              "Only the referrer can toggle the confidentiality of an accompanying course",
            ),
          });
        } else {
          this.$toast.open({ message: "An error occurred" });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
a.flag-toggle {
  color: white;
  cursor: pointer;
  &:hover {
    color: white;
    text-decoration: underline;
    border-radius: 20px;
  }
  i {
    margin: auto 0.4em;
  }
  span.on {
    font-weight: bolder;
  }
}
button.badge {
  &.bg-secondary {
    opacity: 0.5;
    &:hover {
      opacity: 0.7;
    }
  }
}
</style>
