<script setup>
import axios from 'axios'
import {sub} from 'date-fns'

const dataStore = useDataStore()
const accounts = ref([])

//accounts.value = dataStore.emailAccounts.map(i => { return { label: i.emailAddress, emailEngingeId: i.emailEngineId }})

const mailboxes = ref({})
const messages = ref([])
const selectedMailbox = ref("INBOX")
const selectedAccount = ref(null)
const selectedMessage = ref(null)
const setup = async () => {
  accounts.value = dataStore.emailAccounts.map((i,index) => {
    let item = { label: i.emailAddress, emailEngineId: i.emailEngineId }
    if(index === 0) {
      item.defaultOpen = true
    }
    return item

  })

  for await (const account of accounts.value) {
    console.log(account.emailEngineId)
    const {data,error} = await axios.get(`http://157.90.231.142:3000/v1/account/${account.emailEngineId}/mailboxes`, {headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'}})
    console.log(data)
    console.log(error)
    mailboxes.value[account.emailEngineId] = data.mailboxes
  }
}



const selectMailbox = async (account, mailbox) => {
  selectedMailbox.value = mailbox
  const {data,error} = await axios.get(`http://157.90.231.142:3000/v1/account/${account}/messages?path=${ mailbox.path}`, {headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'}})
  console.log(data)
  console.log(error)
  messages.value = data.messages
  selectedAccount.value = account
}
const messageHTML = ref(null)
const messageText = ref(null)
const selectMessage = async (account, message) => {
  console.log(message)
  selectedMessage.value = message
  const {data,error} = await axios.get(`http://157.90.231.142:3000/v1/account/${account}/text/${message.text.id}`, {headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'}})
  messageHTML.value = data.html
  messageText.value = data.plain
}

const addFlags = async (account, message, flags) => {
  console.log(flags)
  const {data,error} = await axios({
    method: "PUT",
    url: `http://157.90.231.142:3000/v1/account/${account}/message/${message.id}`,
    headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'},
    data: {
      flags: {
        add: flags
      }
    }
  })

  console.log(data)
  console.log(error)
}

const removeFlags = async (account, message, flags) => {
  console.log(flags)
  const {data,error} = await axios({
    method: "PUT",
    url: `http://157.90.231.142:3000/v1/account/${account}/message/${message.id}`,
    headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'},
    data: {
      flags: {
        delete: flags
      }
    }
  })

  console.log(data)
  console.log(error)
}

const setSeen = async (seen,message) => {
  if(seen) {
    await addFlags(selectedAccount.value,message, ["\\Seen"])
    await selectMailbox(selectedAccount.value, selectedMailbox.value)
  } else {
    await removeFlags(selectedAccount.value,message, ["\\Seen"])
    await selectMailbox(selectedAccount.value, selectedMailbox.value)
  }
}

const moveTo = async (destinationPath) => {
  const {data,error} = await axios({
    method: "PUT",
    url: `http://157.90.231.142:3000/v1/account/${selectedAccount.value}/message/${selectedMessage.value.id}/move`,
    headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'},
    data: {
      path: destinationPath
    }
  })

  console.log(data)
  console.log(error)
  selectedMessage.value = null
  messageHTML.value = null
  messageText.value = null
  selectMailbox(selectedAccount.value, selectedMailbox.value)
}

const downloadAttachment = async (attachment) => {
  const {data,error} = await axios({
    method: "GET",
    url:   `http://157.90.231.142:3000/v1/account/${selectedAccount.value}/attachment/${attachment.id}`,
    headers: { 'Authorization': 'Bearer dadb572465fba648590f31557f68028a750b47b278d87c1773e8fd09670eec59'},
    responseType: "blob"
  })

  const downloadURL = URL.createObjectURL(new Blob([data]))
  const link = document.createElement('a')
  link.href = downloadURL
  link.setAttribute('download', attachment.filename)
  document.body.appendChild(link)
  link.click()
  link.remove()
    console.log(data)
    console.log(error)
}


setup()

const selectedTab = ref(0)

const mails = ref([{
  id: 1,
  from: {
    name: 'Alex Smith',
    email: 'alex.smith@example.com',
    avatar: {
      src: 'https://i.pravatar.cc/128?u=1'
    }
  },
  subject: 'Meeting Schedule',
  body: 'Hi there, just a quick reminder about our meeting scheduled for 10 AM tomorrow. We\'ll be discussing the new marketing strategies and I would really appreciate your input on the matter. Looking forward to a productive session.',
  date: new Date().toISOString()
}, {
  id: 2,
  unread: true,
  from: {
    name: 'Jordan Brown',
    email: 'jordan.brown@example.com',
    avatar: {
      src: 'https://i.pravatar.cc/128?u=2'
    }
  },
  subject: 'Project Update',
  body: 'I wanted to provide you with the latest update on the project. We\'ve made significant progress on the development front and I\'ve attached a detailed report for your review. Please let me know your thoughts and any areas for improvement.',
  date: sub(new Date(), { minutes: 7 }).toISOString()
}, {
  id: 3,
  unread: true,
  from: {
    name: 'Taylor Green',
    email: 'taylor.green@example.com',
    avatar: {
      src: 'https://i.pravatar.cc/128?u=3'
    }
  },
  subject: 'Lunch Plans',
  body: 'Hey! I was wondering if you would like to grab lunch this Friday. I know a great spot downtown that serves the best Mexican cuisine. It would be a great opportunity for us to catch up and discuss the upcoming team event.',
  date: sub(new Date(), { hours: 3 }).toISOString()
}, {
  id: 4,
  from: {
    name: 'Morgan White',
    email: 'morgan.white@example.com',
    avatar: {
      src: 'https://i.pravatar.cc/128?u=4'
    }
  },
  subject: 'New Proposal',
  body: 'I\'ve attached the new proposal for our next project. It outlines all the objectives, timelines, and resource allocations. I\'m particularly excited about the innovative approach we\'re taking this time. Please have a look and let me know your thoughts.',
  date: sub(new Date(), { days: 1 }).toISOString()
}, {
  id: 5,
  from: {
    name: 'Casey Gray',
    email: 'casey.gray@example.com'
  },
  subject: 'Travel Itinerary',
  body: 'Your travel itinerary for the upcoming business trip is ready. I\'ve included all flight details, hotel reservations, and meeting schedules. Please review and let me know if there are any changes you would like to make or any additional arrangements needed.',
  date: sub(new Date(), { days: 1 }).toISOString()
}, {
  id: 6,
  from: {
    name: 'Jamie Johnson',
    email: 'jamie.johnson@example.com'
  },
  subject: 'Budget Report',
  body: 'I\'ve completed the budget report for this quarter. It includes a detailed analysis of our expenditures and revenue, along with projections for the next quarter. I believe there are some areas where we can optimize our spending. Let\'s discuss this in our next finance meeting.',
  date: sub(new Date(), { days: 2 }).toISOString()
}, {
  id: 7,
  from: {
    name: 'Riley Davis',
    email: 'riley.davis@example.com',
    avatar: {
      src: 'https://i.pravatar.cc/128?u=7'
    }
  },
  subject: 'Training Session',
  body: 'Just a reminder about the training session scheduled for next week. We\'ll be covering new software tools that are crucial for our workflow. It\'s important that everyone attends as this will greatly enhance our team\'s efficiency. Please confirm your availability.',
  date: sub(new Date(), { days: 2 }).toISOString()
}, {
  id: 8,
  unread: true,
  from: {
    name: 'Kelly Wilson',
    email: 'kelly.wilson@example.com',
    avatar: {
      src: 'https://i.pravatar.cc/128?u=8'
    }
  },
  subject: 'Happy Birthday!',
  body: 'Happy Birthday! Wishing you a fantastic day filled with joy and laughter. Your dedication and hard work throughout the year have been invaluable to our team. Enjoy your day to the fullest!',
  date: sub(new Date(), { days: 2 }).toISOString()
}, {
  id: 9,
  from: {
    name: 'Drew Moore',
    email: 'drew.moore@example.com'
  },
  subject: 'Website Feedback',
  body: 'We are in the process of revamping our company website and I would greatly appreciate your feedback on the new design. Your perspective is always insightful and could help us enhance the user experience significantly. Please let me know a convenient time for you to discuss this.',
  date: sub(new Date(), { days: 5 }).toISOString()
}, {
  id: 10,
  from: {
    name: 'Jordan Taylor',
    email: 'jordan.taylor@example.com'
  },
  subject: 'Gym Membership',
  body: 'This is a friendly reminder that your gym membership is due for renewal at the end of this month. We\'ve added several new classes and facilities that I think you\'ll really enjoy. Let me know if you would like a tour of the new facilities.',
  date: sub(new Date(), { days: 5 }).toISOString()
}, {
  id: 11,
  unread: true,
  from: {
    name: 'Morgan Anderson',
    email: 'morgan.anderson@example.com'
  },
  subject: 'Insurance Policy',
  body: 'I\'m writing to inform you that your insurance policy details have been updated. The new document outlines the changes in coverage and premium rates. It\'s important to review these changes to ensure they meet your needs. Please don\'t hesitate to contact me if you have any questions.',
  date: sub(new Date(), { days: 12 }).toISOString()
}, {
  id: 12,
  from: {
    name: 'Casey Thomas',
    email: 'casey.thomas@example.com'
  },
  subject: 'Book Club Meeting',
  body: 'I\'m excited to remind you about our next book club meeting scheduled for next Thursday. We\'ll be discussing \'The Great Gatsby,\' and I\'m looking forward to hearing everyone\'s perspectives. Also, we will be choosing our next book, so bring your suggestions!',
  date: sub(new Date(), { months: 1 }).toISOString()
}, {
  id: 13,
  from: {
    name: 'Jamie Jackson',
    email: 'jamie.jackson@example.com'
  },
  subject: 'Recipe Exchange',
  body: 'Don\'t forget to send in your favorite recipe for our upcoming recipe exchange. It\'s a great opportunity to share and discover new and delicious meals. I\'m particularly excited to try out new dishes and add some variety to my cooking.',
  date: sub(new Date(), { months: 1 }).toISOString()
}, {
  id: 14,
  from: {
    name: 'Riley White',
    email: 'riley.white@example.com'
  },
  subject: 'Yoga Class Schedule',
  body: 'The new schedule for yoga classes is now available. We\'ve added some new styles and adjusted the timings to accommodate more participants. I believe these classes are a great way to relieve stress and stay healthy. Hope to see you there!',
  date: sub(new Date(), { months: 1 }).toISOString()
}, {
  id: 15,
  from: {
    name: 'Kelly Harris',
    email: 'kelly.harris@example.com'
  },
  subject: 'Book Launch Event',
  body: 'I\'m thrilled to invite you to my book launch event next month. It\'s been a journey writing this book    and I\'m eager to share it with you. The event will include a reading session, Q&A, and a signing opportunity. Your support would mean a lot to me.',
  date: sub(new Date(), { months: 1 }).toISOString()
}, {
  id: 16,
  from: {
    name: 'Drew Martin',
    email: 'drew.martin@example.com'
  },
  subject: 'Tech Conference',
  body: 'Join us at the upcoming tech conference where we will be discussing the latest trends and innovations in technology. This is a great opportunity to network with industry leaders and learn about cutting-edge developments. Your participation would greatly contribute to our team\'s knowledge and growth.',
  date: sub(new Date(), { months: 1, days: 4 }).toISOString()
}, {
  id: 17,
  from: {
    name: 'Alex Thompson',
    email: 'alex.thompson@example.com'
  },
  subject: 'Art Exhibition',
  body: 'I wanted to invite you to check out the new art exhibition this weekend. It features some amazing contemporary artists and their latest works. It\'s a great opportunity to immerse yourself in the local art scene and get inspired. Let me know if you\'re interested in going together.',
  date: sub(new Date(), { months: 1, days: 15 }).toISOString()
}, {
  id: 18,
  from: {
    name: 'Jordan Garcia',
    email: 'jordan.garcia@example.com'
  },
  subject: 'Networking Event',
  body: 'I\'m looking forward to seeing you at the networking event next week. It\'s a great chance to connect with professionals from various industries and expand our professional network. There will also be guest speakers discussing key business trends. Your presence would add great value to the discussions.',
  date: sub(new Date(), { months: 1, days: 18 }).toISOString()
}, {
  id: 19,
  from: {
    name: 'Taylor Rodriguez',
    email: 'taylor.rodriguez@example.com'
  },
  subject: 'Volunteer Opportunity',
  body: 'We\'re looking for volunteers for the upcoming community event. It\'s a great opportunity to give back and make a positive impact. There are various roles available, so you can choose something that aligns with your interests and skills. Let me know if you\'re interested and I\'ll provide more details.',
  date: sub(new Date(), { months: 1, days: 25 }).toISOString()
}, {
  id: 20,
  from: {
    name: 'Morgan Lopez',
    email: 'morgan.lopez@example.com'
  },
  subject: 'Car Service Reminder',
  body: 'Just a reminder that your car is due for service next week. Regular maintenance is important to ensure your vehicle\'s longevity and performance. I\'ve included the details of the service center and the recommended services in this email. Feel free to contact them directly to schedule an appointment.',
  date: sub(new Date(), { months: 2 }).toISOString()
}])

const dropdownItems = [[{
  label: 'Mark as unread',
  icon: 'i-heroicons-check-circle'
}, {
  label: 'Mark as important',
  icon: 'i-heroicons-exclamation-circle'
}], [{
  label: 'Star thread',
  icon: 'i-heroicons-star'
}, {
  label: 'Mute thread',
  icon: 'i-heroicons-pause-circle'
}]]

// Filter mails based on the selected tab
const filteredMails = computed(() => {
  if (selectedTab.value === 1) {
    return mails.value.filter(mail => !!mail.unread)
  } else {
    return mails.value
  }


})

const selectedMail = ref(null)

const isMailPanelOpen = computed({
  get () {
    return !!selectedMail.value
  },
  set (value) {
    if (!value) {
      selectedMail.value = null
    }
  }
})


</script>

<template>

<!--  <div class="flex flex-row">
    <div id="mailboxlist">
      <UAccordion
        :items="accounts"
      >
        <template #default="{ item, index, open }">
          <UButton variant="soft" class="mt-3">

            <span class="truncate">{{ item.label }}</span>

            <template #trailing>
              <UIcon
                  name="i-heroicons-chevron-right-20-solid"
                  class="w-5 h-5 ms-auto transform transition-transform duration-200"
                  :class="[open && 'rotate-90']"
              />
            </template>
          </UButton>
        </template>
        <template #item="{ item }">
          <div
              v-for="mailbox in mailboxes[item.emailEngineId]"
              class="my-3"
          >
            <UButton
                @click="selectMailbox(item.emailEngineId, mailbox)"
                variant="outline"
            >
              <span v-if="mailbox.name === 'Trash'">Papierkorb</span>
              <span v-else-if="mailbox.name === 'INBOX'">Eingang</span>
              <span v-else-if="mailbox.name === 'Sent'">Gesendet</span>
              <span v-else-if="mailbox.name === 'Drafts'">Entwürfe</span>
              <span v-else-if="mailbox.name === 'spambucket'">Spam</span>
              <span v-else>{{mailbox.name}}</span>
              <UBadge v-if="mailbox.messages > 0">{{mailbox.messages}}</UBadge>
            </UButton>
          </div>


        </template>
      </UAccordion>

    </div>
    <UDivider orientation="vertical" class="maiLDivider"/>
    <div id="maillist">
      <div
          v-for="message in messages"
          v-if="messages.length > 0"
      >
        <div
            :class="message === selectedMessage ?  ['message','text-primary-500'] : ['message']"
            @click="selectMessage(selectedAccount, message),
                    !message.flags.includes('\\Seen') ? setSeen(true,message) : null"
        >
          <UChip
            position="top-left"
            :show="!message.flags.includes('\\Seen')"
          >
            <h1>{{message.from.name ||message.from.address}}</h1>
          </UChip>

          <h3>{{message.subject}}</h3>
        </div>
        <UDivider class="my-3"/>

      </div>
      <div
        v-else
      >
        Keine E-Mails in diesem Postfach
      </div>


    </div>
    <UDivider orientation="vertical" class="maiLDivider"/>

    <div id="mailcontent" v-if="selectedMessage">
      <Toolbar>
        &lt;!&ndash;<UButton
            @click="setup"
        >Setup</UButton>
        <UButton>+ Neu</UButton>
        <UButton>Sync</UButton>
        <UButton>Papierkorb</UButton>
        <UButton>Weiterleiten</UButton>
        <UButton>Antworten</UButton>&ndash;&gt;
        <UButton
          @click="setSeen(false,selectedMessage)"
        >
          Als Ungelesen markieren
        </UButton>
        <UButton
          @click="moveTo('INBOX.Trash')"
          icon="i-heroicons-trash"
        >

        </UButton>
      </Toolbar>
      <UAlert
        v-if="selectedMessage"
        :title="attachment.filename"
        v-for="attachment in selectedMessage.attachments"
        class="my-3"
        :actions="[{label: 'Download', click:() => {downloadAttachment(attachment)}}]"
      >

      </UAlert>
      <iframe
          v-if="messageHTML"
          style="width: 100%; height: 100%"
          :srcdoc="messageHTML">
      </iframe>
      <pre
          class="text-wrap"
          v-else-if="messageText">
        {{messageText}}
      </pre>
    </div>
  </div>-->

  <UDashboardPanel id="inbox" :width="400" :resizable="{ min: 300, max: 500 }">
    <UDashboardNavbar title="Inbox" >
      <template #right>
        <UTabs v-model="selectedTab" :items="[{label: 'Alle'},{label: 'Ungelesen'}]" :ui="{ wrapper: '', list: { height: 'h-9', tab: { height: 'h-7', size: 'text-[13px]' } } }" />
      </template>
    </UDashboardNavbar>

    <!-- ~/components/inbox/InboxList.vue -->
    <InboxList v-model="selectedMail" :mails="filteredMails" />
  </UDashboardPanel>

  <UDashboardPanel v-model="isMailPanelOpen" collapsible grow side="right">
    <template v-if="selectedMail">
      <UDashboardNavbar>
        <template #toggle>
          <UDashboardNavbarToggle icon="i-heroicons-x-mark" />

          <UDivider orientation="vertical" class="mx-1.5 lg:hidden" />
        </template>

        <template #left>
          <UTooltip text="Archive">
            <UButton icon="i-heroicons-archive-box" color="gray" variant="ghost" />
          </UTooltip>

          <UTooltip text="Move to junk">
            <UButton icon="i-heroicons-archive-box-x-mark" color="gray" variant="ghost" />
          </UTooltip>

<!--          <UDivider orientation="vertical" class="mx-1.5" />-->

<!--          <UPopover :popper="{ placement: 'bottom-start' }">
            <template #default="{ open }">
              <UTooltip text="Snooze" :prevent="open">
                <UButton icon="i-heroicons-clock" color="gray" variant="ghost" :class="[open && 'bg-gray-50 dark:bg-gray-800']" />
              </UTooltip>
            </template>

            <template #panel="{ close }">
              <DatePicker @close="close" />
            </template>
          </UPopover>-->
        </template>

        <template #right>
          <UTooltip text="Reply">
            <UButton icon="i-heroicons-arrow-uturn-left" color="gray" variant="ghost" />
          </UTooltip>

          <UTooltip text="Forward">
            <UButton icon="i-heroicons-arrow-uturn-right" color="gray" variant="ghost" />
          </UTooltip>

          <UDivider orientation="vertical" class="mx-1.5" />

          <UDropdown :items="dropdownItems">
            <UButton icon="i-heroicons-ellipsis-vertical" color="gray" variant="ghost" />
          </UDropdown>
        </template>
      </UDashboardNavbar>

      <!-- ~/components/inbox/InboxMail.vue -->
      <InboxMail :mail="selectedMail" />
    </template>
    <div v-else class="flex-1 hidden lg:flex items-center justify-center">
      <UIcon name="i-heroicons-inbox" class="w-32 h-32 text-gray-400 dark:text-gray-500" />
    </div>
  </UDashboardPanel>

</template>

<style scoped>

</style>