<template>
  <RoundedDialog
    v-model="showDialog"
    scrollable
    class="ma-0 pa-0"
    :width="$vuetify.display.smAndUp ? '940px' : '95%'"
  >
  <v-card>
    <v-card-title>
      <v-container fluid>
        <div class="room-name no-upper">{{ $t("poll_create.title") }}</div>
        <v-btn id="btn-close" icon class="header-button-right" @click.stop="showDialog = false">
          <v-icon>close</v-icon>
        </v-btn>
      </v-container>
    </v-card-title>
    <v-divider></v-divider>
    <v-card-text class="poll-create">
      <div class="text-center">
        <v-form v-model="isValid" ref="pollcreateform">
          <v-container fluid class="poll-create-dialog-content">
            <!-- <v-row cols="12" no-gutters class="ma-0 pa-0">
              <v-col cols="12">
                <v-switch
                  v-model="pollIsDisclosed"
                  inset
                  :label="pollIsDisclosed ? $t('poll_create.poll_disclosed') : $t('poll_create.poll_undisclosed')"
                ></v-switch>
              </v-col>
            </v-row> -->
            <v-row cols="12" no-gutters class="ma-0 pa-0">
              <v-col cols="12" class="ma-0 pa-0">
                <InputControl
                  v-model="pollQuestion"
                  :label="$t('poll_create.question_label')"
                  :disabled="isCreating"
                  :error="$t('poll_create.please_complete')"
                />
              </v-col>
            </v-row>

            <v-row v-for="(answer, index) in pollAnswers" cols="12" :key="answer.id" no-gutters class="ma-0 pa-0">
              <v-col cols="12" class="ma-0 pa-0">
                <InputControl
                  v-model="answer.text"
                  :label="
                    index == 0 ? $t('poll_create.answer_label_1') : $t('poll_create.answer_label_n', { index: index + 1 })
                  "
                  :disabled="isCreating"
                  :error="$t('poll_create.please_complete')"
                  :class="{ deletable: pollAnswers.length > 1 }"
                  ref="answerInput"
                >
                  <v-btn v-if="pollAnswers.length > 1" color="transparent" icon="delete" @click="removeAnswer(index)"></v-btn>
                </InputControl>
              </v-col>
            </v-row>

            <v-row cols="12" no-gutters class="ma-0 pa-0">
              <v-col class="ma-0 pa-0">
                  <div :class="{'add-answer-button':true, 'clickable': true, 'hidden': addingAnswer}" @click.stop="addAnswer">
                    <v-icon>$vuetify.icons.ic_add</v-icon>
                  </div>
              </v-col>
            </v-row>
            <v-row justify="center">
              <v-col cols="auto">
                <v-btn
                  id="btn-create-poll"
                  variant="flat"
                  block
                  class="filled-button publish-button"
                  @click.stop="onCreatePoll()"
                  :disabled="isCreating || !publishButtonEnabled"
                  >{{ $t("poll_create.create") }}</v-btn
                >
              </v-col>
            </v-row>
          </v-container>
        </v-form>
        <div class="poll-create-status">
          {{ status }}
          <v-progress-circular v-if="isCreating" indeterminate color="primary" size="14"></v-progress-circular>
        </div>
      </div>
    </v-card-text>
    <v-card-actions>
      <v-container>
        <v-row>
          <v-col cols="auto">
            <div class="tip">
              <b>{{ $t("poll_create.tip_title") }}</b
              > {{ $t("poll_create.tip_text") }}
            </div>
          </v-col>
        </v-row>
      </v-container>
    </v-card-actions>
  </v-card>
  </RoundedDialog>
</template>
<script>
import util from "../plugins/utils";
import InputControl from "./InputControl.vue";
import RoomDialogBase from "./RoomDialogBase.vue";
import RoundedDialog from "./RoundedDialog.vue";

export default {
  name: "CreatePollDialog",
  extends: RoomDialogBase,
  data() {
    return this.defaultData();
  },
  methods: {
    onOpenDialog() {  
        // Reset values
        let defaults = this.defaultData();
        this.isValid = defaults.isValid;
        this.isCreating = defaults.isCreating;
        this.status = defaults.status;
        this.pollIsDisclosed = defaults.pollIsDisclosed;
        this.pollQuestion = defaults.pollQuestion;
        this.pollAnswers = defaults.pollAnswers;
        this.autoIncrementId = defaults.autoIncrementId;
        this.addingAnswer = defaults.addingAnswer;
        if (this.$refs.pollcreateform) {
          this.$refs.pollcreateform.resetValidation();
        }
    },
    defaultData() {
      return {
        isValid: true,
        isCreating: false,
        status: String.fromCharCode(160),
        pollIsDisclosed: false,
        pollQuestion: null,
        pollAnswers: [
          { id: "1", text: null },
          { id: "2", text: null },
        ],
        autoIncrementId: 2,
        addingAnswer: false,
      };
    },
    addAnswer() {
      if (this.pollAnswers.length < 20) {
        // MAX length according to spec
        this.addingAnswer = true;
        const idx = this.pollAnswers.length;
        this.autoIncrementId += 1;
        this.pollAnswers.push({ id: "" + this.autoIncrementId, text: null });
        this.$nextTick(() => {
          this.$refs.answerInput[idx].focus();
          this.$nextTick(() => {
            this.addingAnswer = false;
          });
        });
      }
    },
    removeAnswer(index) {
      this.pollAnswers.splice(index, 1);
    },
    onCreatePoll() {
      if (this.$refs.pollcreateform.validate()) {
        this.isCreating = true;
        this.status = this.$t("poll_create.creating");
        util
          .createPoll(
            this.$matrix.matrixClient,
            this.room.roomId,
            this.pollQuestion,
            this.pollAnswers,
            this.pollIsDisclosed
          )
          .then(() => {
            console.log("Sent message");
            this.isCreating = false;
            this.showDialog = false;
          })
          .catch((err) => {
            console.log("Failed to send:", err);
            this.isCreating = false;
            this.status = this.$t("poll_create.failed");
          });
        setTimeout(() => {}, 3000);
      } else {
        this.isValid = false;
      }
    },
  },
  computed: {
    publishButtonEnabled() {
      return this.pollQuestion && this.pollAnswers.length > 0 && this.pollAnswers.every(a => a.text);
    }
  },
  components: { InputControl, RoundedDialog },
};
</script>

<style lang="scss">
@use "@/assets/css/chat.scss" as *;
@use "@/assets/css/components/poll.scss";
</style>
