<script setup>
import { useTemplateRef, nextTick } from 'vue'
import BigIcon from './BigIcon.vue'
import BookDateWidget from './BookDateWidget.vue'
import DateWidget from './DateWidget.vue'

const props = defineProps({
  wantread: Boolean,
  startReadDate: String,
  endReadDate: String,
  read: Boolean,
})

const mobiledatesel = useTemplateRef('mobiledates')

const emit = defineEmits([
  'onWantReadIconClick',
  'onStartReadIconClick',
  'onReadIconClick',
  'onStartReadDateChange',
  'onEndReadDateChange',
])

function isStartReadExpanded() {
  let isStartReadDateSet = props.startReadDate ? true : false
  let isReadUnset = !props.read ? true : false
  return isStartReadDateSet && isReadUnset
}

async function onReadIconClick() {
  emit('onReadIconClick')
  await nextTick()
  mobiledatesel.value.scrollIntoView()
}

async function onStartReadIconClick() {
  emit('onStartReadIconClick')
  await nextTick()
  mobiledatesel.value.scrollIntoView()
}
</script>

<template>
  <div class="iconscontainer" :class="props.read ? 'remove-border-bottom' : ''">
    <div
      class="bigiconcontainer"
      :class="props.wantread ? 'has-text-dark has-background-text border-radius-wantread-fill' : ''"
    >
      <BigIcon
        icon="BIconEye"
        :legend="$t('bookform.wantread')"
        :isSet="props.wantread"
        @click="$emit('onWantReadIconClick')"
      />
    </div>
    <div
      class="bigiconcontainer is-hidden-desktop"
      :class="isStartReadExpanded() ? 'has-text-dark has-background-text' : ''"
    >
      <BigIcon
        icon="BIconBook"
        :legend="$t('bookform.wantread')"
        :is-set="isStartReadExpanded()"
        @click="onStartReadIconClick"
      />
    </div>
    <div
      class="bigiconcontainer is-hidden-desktop"
      :class="props.read ? 'has-text-dark has-background-text border-radius-right-fill' : ''"
    >
      <BigIcon
        icon="BIconCheckCircle"
        :legend="$t('bookform.read')"
        :isSet="props.read"
        @click="onReadIconClick"
      />
    </div>
    <BookDateWidget
      class="is-hidden-mobile"
      icon="BIconBook"
      :legend="$t('bookform.startread')"
      :start-read-date="props.startReadDate"
      :is-expanded="isStartReadExpanded()"
      @onStartDateChange="(d) => $emit('onStartReadDateChange', d)"
      @onIconClick="onStartReadIconClick"
    />
    <BookDateWidget
      class="is-hidden-mobile"
      icon="BIconCheckCircle"
      :legend="$t('bookform.read')"
      :start-read-date="props.startReadDate"
      use-end-date
      last-widget
      :endReadDate="props.endReadDate"
      :isExpanded="props.read"
      @onStartDateChange="(d) => $emit('onStartReadDateChange', d)"
      @onEndDateChange="(d) => $emit('onEndReadDateChange', d)"
      @onIconClick="onReadIconClick"
    />
  </div>

  <div ref="mobiledates" class="mobile-dates pt-3 is-hidden-desktop">
    <div class="mobiledate">
      <DateWidget
        v-if="isStartReadExpanded() || props.read"
        dateinputid="startread"
        dateinputlabel="bookdatewidget.started"
        :initdate="props.startReadDate"
        is-horizontal
        @onDateChange="(d) => $emit('onStartReadDateChange', d)"
      />
    </div>
    <div class="mobiledate pt-2">
      <DateWidget
        v-if="props.read"
        dateinputid="endread"
        dateinputlabel="bookdatewidget.finished"
        :initdate="props.endReadDate"
        is-horizontal
        @onDateChange="(d) => $emit('onEndReadDateChange', d)"
      />
    </div>
  </div>
</template>

<style scoped>
.iconscontainer {
  border: solid;
  border-radius: 50px;
  width: 250px;
}

.mobile-dates {
  display: block;
}

.mobiledate {
  display: flex;
  justify-content: center;
  align-items: center;
}

@media (min-width: 1024px) {
  .border-radius-wantread-fill {
    border-radius: 45px 45px 0px 0px;
  }

  .remove-border-bottom {
    border-bottom: none;
  }

  .bigiconcontainer {
    display: flex;
  }
}

@media (max-width: 1024px) {
  .border-radius-wantread-fill {
    border-radius: 38px 0px 0px 38px;
  }
  .bigiconcontainer {
    flex: 1;
  }

  .iconscontainer {
    display: flex;
    width: 100%;
  }

  .border-radius-right-fill {
    border-radius: 0px 38px 38px 0px;
  }
}
</style>
