<template>
<td
	ref="rootEl"
	:class="$style.cell"
	:style="{ maxWidth: cellWidth, minWidth: cellWidth }"
	:tabindex="-1"
	@dblclick="onCellDoubleClick"
	@keydown="onCellKeyDown"
>
	<div
		:class="[
			$style.root,
			[(cell.violation.valid || cell.selected) ? {} : $style.error],
			[cell.selected ? $style.selected : {}],
			// 行が選択されているときは範囲選択色の適用を行側に任せる
			[(cell.ranged && !cell.row.ranged) ? $style.ranged : {}],
			[needsContentCentering ? $style.center : {}],
		]"
	>
		<div v-if="!editing" ref="contentAreaEl">
			<div :class="$style.content">
				<div v-if="cellType === 'text'">
					{{ cell.value }}
				</div>
				<div v-else-if="cellType === 'boolean'">
					<span v-if="cell.value === true" class="ti ti-check"/>
					<span v-else class="ti"/>
				</div>
				<div v-else-if="cellType === 'image'">
					<img
						:src="cell.value as string"
						:alt="cell.value as string"
						:class="$style.viewImage"
						@load="emitContentSizeChanged"
					/>
				</div>
			</div>
		</div>
		<div v-else ref="inputAreaEl">
			<input
				v-if="cellType === 'text'"
				type="text"
				:class="$style.editingInput"
				:value="editingValue"
				@input="onInputText"
			/>
		</div>
	</div>
</td>
</template>

<script setup lang="ts">
import { computed, defineAsyncComponent, nextTick, onMounted, onUnmounted, ref, shallowRef, toRefs, watch } from 'vue';
import { GridEventEmitter, GridSetting, Size } from '@/components/grid/grid.js';
import { useTooltip } from '@/scripts/use-tooltip.js';
import * as os from '@/os.js';
import { CellValue, GridCell } from '@/components/grid/cell.js';
import { equalCellAddress, getCellAddress } from '@/components/grid/grid-utils.js';

const emit = defineEmits<{
	(ev: 'operation:beginEdit', sender: GridCell): void;
	(ev: 'operation:endEdit', sender: GridCell): void;
	(ev: 'change:value', sender: GridCell, newValue: CellValue): void;
	(ev: 'change:contentSize', sender: GridCell, newSize: Size): void;
}>();
const props = defineProps<{
	cell: GridCell,
	gridSetting: GridSetting,
	bus: GridEventEmitter,
}>();

const { cell, gridSetting, bus } = toRefs(props);

const rootEl = shallowRef<InstanceType<typeof HTMLTableCellElement>>();
const contentAreaEl = shallowRef<InstanceType<typeof HTMLDivElement>>();
const inputAreaEl = shallowRef<InstanceType<typeof HTMLDivElement>>();

const editing = ref<boolean>(false);
const editingValue = ref<CellValue>(undefined);

const cellWidth = computed(() => cell.value.column.width);
const cellType = computed(() => cell.value.column.setting.type);
const needsContentCentering = computed(() => {
	switch (cellType.value) {
		case 'boolean':
			return true;
		default:
			return false;
	}
});

watch(() => [cell.value.value], () => {
	// 中身がセットされた直後はサイズが分からないので、次のタイミングで更新する
	nextTick(emitContentSizeChanged);
}, { immediate: true });

watch(() => cell.value.selected, () => {
	if (cell.value.selected) {
		rootEl.value?.focus();
	}
});

function onCellDoubleClick(ev: MouseEvent) {
	switch (ev.type) {
		case 'dblclick': {
			beginEditing();
			break;
		}
	}
}

function onOutsideMouseDown(ev: MouseEvent) {
	const isOutside = ev.target instanceof Node && !rootEl.value?.contains(ev.target);
	if (isOutside || !equalCellAddress(cell.value.address, getCellAddress(ev.target as HTMLElement, gridSetting.value))) {
		endEditing(true);
	}
}

function onCellKeyDown(ev: KeyboardEvent) {
	if (!editing.value) {
		ev.preventDefault();
		switch (ev.code) {
			case 'NumpadEnter':
			case 'Enter':
			case 'F2': {
				beginEditing();
				break;
			}
		}
	} else {
		switch (ev.code) {
			case 'Escape': {
				endEditing(false);
				break;
			}
			case 'NumpadEnter':
			case 'Enter': {
				if (!ev.isComposing) {
					endEditing(true);
				}
			}
		}
	}
}

function onInputText(ev: Event) {
	editingValue.value = (ev.target as HTMLInputElement).value;
}

function onForceRefreshContentSize() {
	emitContentSizeChanged();
}

function registerOutsideMouseDown() {
	unregisterOutsideMouseDown();
	addEventListener('mousedown', onOutsideMouseDown);
}

function unregisterOutsideMouseDown() {
	removeEventListener('mousedown', onOutsideMouseDown);
}

function beginEditing() {
	if (editing.value || !cell.value.column.setting.editable) {
		return;
	}

	switch (cellType.value) {
		case 'text': {
			editingValue.value = cell.value.value;
			editing.value = true;
			registerOutsideMouseDown();
			emit('operation:beginEdit', cell.value);

			nextTick(() => {
				// inputの展開後にフォーカスを当てたい
				if (inputAreaEl.value) {
					(inputAreaEl.value.querySelector('*') as HTMLElement).focus();
				}
			});
			break;
		}
		case 'boolean': {
			// とくに特殊なUIは設けず、トグルするだけ
			emitValueChange(!cell.value.value);
			break;
		}
	}
}

function endEditing(applyValue: boolean) {
	if (!editing.value) {
		return;
	}

	emit('operation:endEdit', cell.value);
	unregisterOutsideMouseDown();

	if (applyValue) {
		emitValueChange(editingValue.value);
	}

	editingValue.value = undefined;
	editing.value = false;

	rootEl.value?.focus();
}

function emitValueChange(newValue: CellValue) {
	const _cell = cell.value;
	emit('change:value', _cell, newValue);
}

function emitContentSizeChanged() {
	emit('change:contentSize', cell.value, {
		width: contentAreaEl.value?.clientWidth ?? 0,
		height: contentAreaEl.value?.clientHeight ?? 0,
	});
}

useTooltip(rootEl, (showing) => {
	if (cell.value.violation.valid) {
		return;
	}

	const content = cell.value.violation.violations.map(it => it.result.message).join('\n');
	os.popup(defineAsyncComponent(() => import('@/components/grid/MkCellTooltip.vue')), {
		showing,
		content,
		targetElement: rootEl.value,
	}, {}, 'closed');
});

onMounted(() => {
	bus.value.on('forceRefreshContentSize', onForceRefreshContentSize);
});

onUnmounted(() => {
	bus.value.off('forceRefreshContentSize', onForceRefreshContentSize);
});

</script>

<style module lang="scss">
$cellHeight: 28px;

.cell {
	overflow: hidden;
	white-space: nowrap;
	height: $cellHeight;
	max-height: $cellHeight;
	min-height: $cellHeight;
	cursor: cell;

	&:focus {
		outline: none;
	}
}

.root {
	display: flex;
	flex-direction: row;
	align-items: center;
	box-sizing: border-box;
	height: 100%;

	// selected適用時に中身がズレてしまうので、透明の線をあらかじめ引いておきたい
	border: solid 0.5px transparent;

	&.selected {
		border: solid 0.5px var(--accentLighten);
	}

	&.ranged {
		background-color: var(--accentedBg);
	}

	&.center {
		justify-content: center;
	}

	&.error {
		border: solid 0.5px var(--error);
	}
}

.content {
	display: inline-block;
	padding: 0 8px;
}

.viewImage {
	width: auto;
	max-height: $cellHeight;
	height: $cellHeight;
	object-fit: cover;
}

.editingInput {
	padding: 0 8px;
	width: 100%;
	max-width: 100%;
	box-sizing: border-box;
	min-height: $cellHeight;
	max-height: $cellHeight;
	height: $cellHeight + 4px;
	outline: none;
	border: none;
	font-family: 'Hiragino Maru Gothic Pro', "BIZ UDGothic", Roboto, HelveticaNeue, Arial, sans-serif;
}

</style>
