<template>
  <div class="welcome-card">
    <div class="welcome-title">
      <div class="welcome-title-left-container">
        <image v-if="isTikTok" :src="iconQuestion" class="img-in-tt"></image>
        <Icon v-else :src="iconQuestion" />
        <p
          v-if="title"
          class="card-title"
        >
          {{ title }}
        </p>
      </div>
      <div
        class="change-wrapper"
        v-if="list.length > 3 && !showAllRobotWelcomeItems"
        @click="changeBranchList()"
      >
        <image v-if="isTikTok" :src="iconRefresh" class="img-in-tt"></image>
        <Icon v-else :src="iconRefresh" />
      </div>
    </div>
    <div class="welcome-items-container">
      <div
        v-for="(item, index) in showList"
        :key="index"
        class="welcome-item"
        @click="handleContentListItemClick(item)"
      >
        <div class="item-container">
          <div class="item-content">{{ item.content }}</div>
        </div>
      </div>
      <div class="welcome-item welcome-item-hidden">
        {{ longestContent }}
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import vue from '../../../../../../adapter-vue';
import imRobotGuess from '../../../../../../assets/imRobotGuess.svg';
import refresh from '../../../../../../assets/refresh.svg';
import iconRight from '../../../../../../assets/iconRight.svg';
import Icon from './customer-icon.vue';
import iconQuestion from '../../../../../../assets/icon_question.png';
import iconRefresh from '../../../../../../assets/icon_refresh.png';
import { customerServicePayloadType } from '../../../../../../interface';
import { isTikTok } from "../../../../../../utils/env";
import state from '../../../../../../utils/state.js';

const { reactive, toRefs, computed } = vue;

interface Props {
  payload: customerServicePayloadType;
}

interface welcomeBranchItem {
  id: string;
  content: string;
  answer: string;
}

export default {
  components: {
    Icon,
  },
  props: {
    payload: {
      type: Object as () => customerServicePayloadType,
      default: () => ({ content: { title: '', items: [] }}),
    },
  },
  emits: ['sendMessage'],
  setup(props: Props, { emit }) {
    const showAllRobotWelcomeItems = state.get('showAllRobotWelcomeItems');
    const welcomeItemList = props.payload.content.items || [];
    const data = reactive({
      // title
      title: props.payload.content.title || '',
      // all branch list
      list: welcomeItemList,
      // current branch list
      showList: showAllRobotWelcomeItems ? welcomeItemList : welcomeItemList.slice(0, 3),
      // current page number
      pageNumber: 1,
    });

    const handleContentListItemClick = (branch: welcomeBranchItem) => {
      emit('sendMessage', { text: branch.content });
    };

    const changeBranchList = () => {
      if (data.pageNumber * 3 >= data.list?.length) {
        data.pageNumber = 0;
      }
      data.showList = data.list?.slice(
        data.pageNumber * 3,
        data.pageNumber * 3 + 3,
      );
      data.pageNumber += 1;
    };

    const longestContent = computed(() => {
      if (!data.list) {
        return '';
      }
      let longestContent = '';
      data.list.forEach((item) => {
        if (item.content.length > longestContent.length) {
          longestContent = item.content;
        }
      });
      return longestContent;
    });

    return {
      ...toRefs(data),
      handleContentListItemClick,
      imRobotGuess,
      refresh,
      iconRight,
      changeBranchList,
      iconQuestion,
      iconRefresh,
      longestContent,
      isTikTok,
      showAllRobotWelcomeItems,
    };
  },
};
</script>

<style lang="scss">
.welcome-card {
  max-width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .welcome-title {
    display: flex;
    height: 20px;
    margin-bottom: 10px;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 10px;
  }

  .welcome-title-left-container {
    display: flex;
    align-items: center;
  }

  .card-title {
    display: inline-block;
    margin-left: 8px;
    font-size: 14px;
  }

  .change-wrapper {
    display: flex;
    cursor: pointer;
  }

  .welcome-items-container {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .welcome-item {
      width: 100%;
      padding: 6px 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      background-color: #ffffff;
      margin-bottom: 10px;
      border: 1px solid #adcfff;
      border-radius: 20px;
      font-weight: 500;
      color: #1c66e5;
      gap: 10px;
      .item-container {
        text-align: center;
        max-width: 100%;
        .item-content {
          display: inline-block;
          text-align: left;
          overflow-wrap: break-word;
          word-break: normal;
          width: 100%;
        }
      }
    }

    .welcome-item:hover {
      background: #f2f7ff;
    }

    .welcome-item:last-child {
      margin-bottom: 0;
    }

    .welcome-item-hidden {
      visibility: hidden;
      padding: 0;
      margin: 0;
      height: 0;
    }
  }
}

.img-in-tt {
  width: 16px;
  height: 16px;
}

</style>
