import { _ } from '../../locale';
import CommandService from '../CommandService';
import { ItemFlow, ListRenderer, OnClickEvent } from '../plugins/api/noteListType';
import checkboxPieCss from './checkboxPieCss';

interface CheckboxStats {
	total: number;
	checked: number;
	percent: number;
	isComplete: boolean;
}

interface Props {
	note: {
		id: string;
		title: string;
		is_todo: number;
		todo_completed: number;
		checkboxes: CheckboxStats | null;
	};
	item: {
		// index: number;
		size: {
			height: number;
		};
		selected: boolean;
	};
}

const renderer: ListRenderer = {
	id: 'compact',

	label: async () => _('Compact'),

	flow: ItemFlow.TopToBottom,

	itemSize: {
		width: 0,
		height: 34,
	},

	dependencies: [
		// 'item.index',
		'item.selected',
		'item.size.height',
		'note.checkboxes',
		'note.id',
		'note.is_shared',
		'note.is_todo',
		'note.isWatched',
		'note.title',
		'note.todo_completed',
		'note.todoStatusText',
	],

	itemCss: // css
		`	
		&:before {
			content: '';
			border-bottom: 1px solid var(--joplin-divider-color);
			width: 90%;
			position: absolute;
			bottom: 0;
			left: 5%;
		}
	
		> .content.-selected {
			background-color: var(--joplin-selected-color);
		}

		&:hover, &.-focus-visible > .content {
			background-color: var(--joplin-background-color-hover3);
		}
	
		> .content {
			display: flex;
			box-sizing: border-box;
			position: relative;
			width: 100%;
			padding-left: 16px;
	
			> .checkbox {
				display: flex;
				align-items: center;

				> input {
					margin: 0px 10px 1px 0px;
				}
			}
	
			> .title {
				font-family: var(--joplin-font-family);
				font-size: var(--joplin-font-size);
				text-decoration: none;
				color: var(--joplin-color);
				cursor: default;
				white-space: nowrap;
				flex: 1 1 0%;
				display: flex;
				align-items: center;
				overflow: hidden;

				> .watchedicon {
					display: none;
					padding-right: 4px;
					color: var(--joplin-color);
				}
			}

			> .checkbox-pie {
				display: flex;
				align-items: center;
				padding-right: 12px;
				padding-left: 8px;
			}
		}

		${checkboxPieCss}

		> .content.-shared {
			> .title {
				color: var(--joplin-color-warn3);
			}
		}

		> .content.-completed {
			> .title {
				opacity: 0.5;
			}
		}

		> .content.-watched {
			> .title {
				> .watchedicon {
					display: inline;
				}
			}
		}
	`,

	headerTemplate: // html
		`
		<button data-id="title">Title</button><button data-id="updated">Updated</button>
	`,

	onHeaderClick: async (event: OnClickEvent) => {
		const field = event.elementId === 'title' ? 'title' : 'user_updated_time';
		void CommandService.instance().execute('toggleNotesSortOrderField', field);
	},

	itemTemplate: // html
		`
		<div class="content {{#item.selected}}-selected{{/item.selected}} {{#note.is_shared}}-shared{{/note.is_shared}} {{#note.todo_completed}}-completed{{/note.todo_completed}} {{#note.isWatched}}-watched{{/note.isWatched}}">
			{{#note.is_todo}}
				<div class="checkbox">
					<input
						data-id="todo-checkbox"
						type="checkbox"
						aria-label="{{note.todoStatusText}}"
						tabindex="-1"
						{{#note.todo_completed}}checked="checked"{{/note.todo_completed}}
					>
				</div>
			{{/note.is_todo}}
			<div class="title" data-id="{{note.id}}">
				<i class="watchedicon fa fa-share-square"></i>
				<span>{{note.title}}</span>
			</div>
			{{#checkboxStats}}
				<div class="checkbox-pie" title="{{checked}}/{{total}}">
					{{#isComplete}}
						<div class="pie -complete">✓</div>
					{{/isComplete}}
					{{^isComplete}}
						<div class="pie" style="--percent: {{percent}};"></div>
					{{/isComplete}}
				</div>
			{{/checkboxStats}}
		</div>
	`,

	onRenderNote: async (props: Props) => {
		return {
			...props,
			checkboxStats: props.note.checkboxes,
		};
	},
};

export default renderer;
