<template>
  <div ref="el" class="fdidabkb" :class="classes" :style="{ background: bg }" @click="onClick">
    <div v-show="narrow" class="buttons left" @click="openAccountMenu">
      <MkAvatar v-if="displayMyAvatar && user" class="avatar" :user="user" :disable-preview="true"/>
    </div>
    <template v-if="hasTabs">
      <div v-show="!hideTitle" class="titleContainer" @click="showTabsPopup">
        <MkAvatar v-if="metadata.avatar" class="avatar" :user="metadata.avatar" :disable-preview="true" :show-indicator="true"/>
        <i v-else-if="metadata.icon && !narrow" class="icon" :class="metadata.icon"></i>
        <div class="title">
          <MkUserName v-if="metadata.userName" :user="metadata.userName" :nowrap="true" class="title"/>
          <div v-else-if="metadata.title && !hasTabs && !narrow" class="title">{{ metadata.title }}</div>
          <div v-show="!narrow && metadata.subtitle" class="subtitle">{{ metadata.subtitle }}</div>
        </div>
      </div>
      <div ref="tabsEl" class="tabs" v-show="hasTabs">
        <button v-for="tab in tabs" :key="tab.key" ref="tabRefs[tab.key]" v-tooltip.noDelay="tab.title"
          class="tab _button" :class="{ active: isActiveTab(tab) }" @mousedown="onTabMousedown(tab)"
          @click="onTabClick(tab)">
          <i v-if="tab.icon && !tab.iconOnly" class="icon" :class="tab.icon"></i>
          <span class="title">{{ tab.title }}</span>
        </button>
        <div ref="tabHighlightEl" class="highlight"></div>
      </div>
    </template>
    <div v-show="hasActions" class="buttons right">
      <button v-for="(action, index) in actions" :key="index" v-tooltip.noDelay="action.text"
        class="_button button" :class="{ highlighted: action.highlighted }" @click.stop="action.handler"
        @touchstart="preventDrag">
        <i :class="action.icon"></i>
      </button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, inject, watch, shallowReactive, nextTick } from 'vue';
import tinycolor from 'tinycolor2';
import { popupMenu } from '@/os';
import { scrollToTop } from '@/scripts/scroll';
import { globalEvents } from '@/events';
import { injectPageMetadata } from '@/scripts/page-metadata';
import { $i, openAccountMenu as openAccountMenu_ } from '@/account';

type Tab = {
	key?: string | null;
	title: string;
	icon?: string;
	iconOnly?: boolean;
	onClick?: (ev: MouseEvent) => void;
};

const props = defineProps<{
	tabs?: Tab[];
	tab?: string;
	actions?: {
		text: string;
		icon: string;
		handler: (ev: MouseEvent) => void;
	}[];
	thin?: boolean;
	displayMyAvatar?: boolean;
}>();

const emit = defineEmits<{
	(ev: 'update:tab', key: string);
}>();

const metadata = injectPageMetadata();

const hideTitle = inject('shouldOmitHeaderTitle', false);
const thin_ = props.thin || inject('shouldHeaderThin', false);

const el = ref<HTMLElement | null>(null);
const tabRefs: Record<string, HTMLElement> = {};
const tabHighlightEl = ref<HTMLElement | null>(null);
const tabsEl = ref<HTMLElement | null>(null);
const bg = ref<string | null>(null);
const narrow = ref(false);
const height = ref(0);
const hasTabs = computed(() => props.tabs && props.tabs.length > 0);
const hasActions = computed(() => props.actions && props.actions.length > 0);
const show = computed(() => {
	return !hideTitle || hasTabs.value || hasActions.value;
});

const openAccountMenu = (ev: MouseEvent) => {
	openAccountMenu_({
		withExtraOperation: true,
	}, ev);
};

const showTabsPopup = (ev: MouseEvent) => {
	if (!hasTabs.value) return;
	if (!narrow.value) return;
	ev.preventDefault();
	ev.stopPropagation();
	const menu = props.tabs!.map(tab => ({
		text: tab.title,
		icon: tab.icon,
		active: tab.key != null && tab.key === props.tab,
		action: (ev) => {
			onTabClick(tab, ev);
		},
	}));
	popupMenu(menu, ev.currentTarget ?? ev.target);
};

const preventDrag = (ev: TouchEvent) => {
	ev.stopPropagation();
};

const onClick = () => {
	scrollToTop(el.value!, { behavior: 'smooth' });
};

function onTabMousedown(tab: Tab, ev: MouseEvent): void {
	// ユーザビリティの観点からmousedown時にはonClickは呼ばない
	if (tab.key) {
		emit('update:tab', tab.key);
	}
}

function onTabClick(tab: Tab, ev: MouseEvent): void {
	if (tab.onClick) {
		ev.preventDefault();
		ev.stopPropagation();
		tab.onClick(ev);
	}
	if (tab.key) {
		emit('update:tab', tab.key);
	}
}

const calcBg = () => {
	const rawBg = metadata?.bg || 'var(--bg)';
	const tinyBg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg);
	tinyBg.setAlpha(0.85);
	bg.value = tinyBg.toRgbString();
};

let ro: ResizeObserver | null;

onMounted(() => {
  calcBg();
  globalEvents.on('themeChanged', calcBg);

  const updateTabHighlight = () => {
    nextTick(() => {
      const tabEl = tabRefs[props.tab];
      if (tabEl && tabHighlightEl) {
        const tabSizeX = tabEl.scrollWidth + 20;
        tabEl.style.setProperty('--width', `${tabSizeX}px`);

        const parentRect = tabsEl.getBoundingClientRect();
        const rect = tabEl.getBoundingClientRect();
        const left = (rect.left - parentRect.left + tabsEl.scrollLeft);
        tabHighlightEl.style.width = `${tabSizeX}px`;
        tabHighlightEl.style.transform = `translateX(${left}px)`;
        tabsEl.scrollTo({left: left - 60, behavior: "smooth"});
      }
    });
  };

  const updateTab = () => {
    emit('update:tab', props.tab);
  };

  watch(() => [props.tab, props.tabs], updateTabHighlight, { immediate: true });
  watch(() => props.tab, updateTab);

  if (el && el.parentElement) {
    narrow.value = el.parentElement.offsetWidth < 500;
    ro = new ResizeObserver((entries, observer) => {
      if (el.parentElement && document.body.contains(el)) {
        narrow.value = el.parentElement.offsetWidth < 500;
      }
    });
    ro.observe(el.parentElement);
  }
});

onUnmounted(() => {
	globalEvents.off('themeChanged', calcBg);
	if (ro) ro.disconnect();
});
</script>

<style lang="scss" scoped>
.fdidabkb {
	--height: 55px;
	display: flex;
	width: 100%;
	-webkit-backdrop-filter: var(--blur, blur(15px));
	backdrop-filter: var(--blur, blur(15px));
	border-bottom: solid 0.5px var(--divider);
	height: var(--height);

	&.thin {
		--height: 45px;

		> .buttons {
			> .button {
				font-size: 0.9em;
			}
		}
	}

	&.slim {
		> .titleContainer {
			flex: 1;
			margin: 0 auto;

			> *:first-child {
				margin-left: auto;
			}

			> *:last-child {
				margin-right: auto;
			}
		}
		> .tabs {
			padding-inline: 12px;
			mask: linear-gradient(to right, black 80%, transparent);
			-webkit-mask: linear-gradient(to right, black 80%, transparent);
			scrollbar-width: none;
			&::-webkit-scrollbar {
				display: none;
			}
			&::after { // Force right padding
				content: "";
				display: inline-block;
				min-width: 20%;
			}
		}
	}

	> .buttons {
		--margin: 8px;
		display: flex;
		align-items: center;
		height: var(--height);
		margin: 0 var(--margin);

		&.left {
			margin-right: auto;

			> .avatar {
				$size: 32px;
				display: inline-block;
				width: $size;
				height: $size;
				vertical-align: bottom;
				margin: 0 8px;
				pointer-events: none;
			}
		}

		&.right {
			margin-left: auto;
		}

		&:empty {
			display: none;
		}

		> .button {
			display: flex;
			align-items: center;
			justify-content: center;
			height: calc(var(--height) - (var(--margin) * 2));
			width: calc(var(--height) - (var(--margin) * 2));
			box-sizing: border-box;
			position: relative;
			border-radius: 5px;

			&:hover {
				background: rgba(0, 0, 0, 0.05);
			}

			&.highlighted {
				color: var(--accent);
			}
		}

		> .fullButton {
			& + .fullButton {
				margin-left: 12px;
			}
		}
	}

	> .titleContainer {
		display: flex;
		align-items: center;
		max-width: 400px;
		overflow: auto;
		white-space: nowrap;
		text-align: left;
		font-weight: bold;
		flex-shrink: 0;
		margin-left: 24px;
		margin-right: 1rem;

		> .avatar {
			$size: 32px;
			display: inline-block;
			width: $size;
			height: $size;
			vertical-align: bottom;
			margin: 0 8px;
			pointer-events: none;
		}

		> .icon {
			margin-right: 8px;
			width: 16px;
			text-align: center;
			transform: translate(0em);
		}

		> .title {
			min-width: 0;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			line-height: 1.1;

			> .subtitle {
				opacity: 0.6;
				font-size: 0.8em;
				font-weight: normal;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;

				&.activeTab {
					text-align: center;

					> .chevron {
						display: inline-block;
						margin-left: 6px;
					}
				}
			}
		}
	}

	> .tabs {
		position: relative;
		width: 100%;
		font-size: 1em;
		overflow-x: auto;
		white-space: nowrap;
		contain: strict;

		> .tab {
			display: inline-flex;
			align-items: center;
			position: relative;
			border-inline: 10px solid transparent;
			height: 100%;
			font-weight: normal;
			opacity: 0.7;
			width: 38px;
			--width: 38px;
			overflow: hidden;
			transition: color .2s, opacity .2s, width .2s;

			&:hover {
				opacity: 1;
			}

			&.active {
				opacity: 1;
				color: var(--accent);
				font-weight: 600;
				width: var(--width);
			}
			&:not(.active) > .title {
				opacity: 0;
			}

			> .icon + .title {
				margin-left: 8px;
			}
			> .title {
				transition: opacity .2s;
			}
		}

		> .highlight {
			position: absolute;
			bottom: 0;
			left: 0;
			height: 3px;
			background: var(--accent);
			border-radius: 999px;
			transition: width .2s, transform .2s;
			transition-timing-function: cubic-bezier(0,0,0,1.2);
			pointer-events: none;
		}
	}
}
</style>
