package templates

import (
	"fmt"
	"mal/internal/database"
)

templ Watchlist(entries []database.GetUserWatchListRow, layout string, currentStatus string, sortBy string, sortOrder string) {
	@Layout("My Watchlist") {
		<div class="watchlist-header">
			<h2>Watchlist</h2>
			<div class="watchlist-controls">
				<a href="/api/watchlist/export" class="text-link">Export</a>
				<button class="text-link" onclick="document.getElementById('import-file').click()">Import</button>
				<form id="import-form" hx-post="/api/watchlist/import" hx-encoding="multipart/form-data" style="display: none;">
					<input type="file" id="import-file" name="file" accept=".json" onchange="htmx.trigger('#import-form', 'submit')"/>
				</form>
				<div class="view-toggle">
					<a href={ templ.URL(fmt.Sprintf("/watchlist?view=grid&status=%s&sort=%s&order=%s", currentStatus, sortBy, sortOrder)) } class={ viewClass(layout == "grid") }>Grid</a>
					<a href={ templ.URL(fmt.Sprintf("/watchlist?view=table&status=%s&sort=%s&order=%s", currentStatus, sortBy, sortOrder)) } class={ viewClass(layout == "table") }>Table</a>
				</div>
			</div>
		</div>
		<div class="status-tabs">
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=all&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "all") }>All</a>
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=watching&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "watching") }>Watching</a>
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=continuing&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "continuing") }>Continuing</a>
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=on_hold&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "on_hold") }>On hold</a>
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=plan_to_watch&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "plan_to_watch") }>Plan to watch</a>
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=dropped&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "dropped") }>Dropped</a>
			<a href={ templ.URL(fmt.Sprintf("/watchlist?view=%s&status=completed&sort=%s&order=%s", layout, sortBy, sortOrder)) } class={ tabClass(currentStatus == "completed") }>Completed</a>
		</div>
		@SortFilter(SortFilterOptions{Sort: sortBy, Order: sortOrder, View: layout, Status: currentStatus})
		if len(entries) == 0 {
			<div class="empty-state">
				<div class="empty-state-title">Nothing here yet</div>
				<div class="empty-state-text">
					if currentStatus == "all" {
						Your watchlist is empty. <a href="/">Search for anime</a> to get started.
					} else if currentStatus == "continuing" {
						No airing anime with watching or plan to watch status.
					} else {
						No anime in this category.
					}
				</div>
			</div>
		} else {
			if layout == "grid" {
				<div class="catalog-grid">
					for _, entry := range entries {
						<div class="catalog-item watchlist-item" id={ fmt.Sprintf("watchlist-entry-%d", entry.AnimeID) }>
							<a href={ templ.URL(fmt.Sprintf("/anime/%d", entry.AnimeID)) }>
								if entry.ImageUrl != "" {
									<img src={ entry.ImageUrl } alt={ entry.DisplayTitle() } class="catalog-thumb" loading="lazy"/>
								} else {
									<div class="no-image">No image</div>
								}
							</a>
							<div class="catalog-title">{ entry.DisplayTitle() }</div>
							<button
								class="remove-btn"
								hx-delete={ string(templ.URL(fmt.Sprintf("/api/watchlist/%d?from=watchlist", entry.AnimeID))) }
								hx-target={ fmt.Sprintf("#watchlist-entry-%d", entry.AnimeID) }
								hx-swap="delete"
							>&times;</button>
						</div>
					}
				</div>
			} else {
				<table class="watchlist-table">
					<thead>
						<tr>
							<th></th>
							<th>Title</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
						for _, entry := range entries {
							<tr id={ fmt.Sprintf("watchlist-entry-%d", entry.AnimeID) }>
								<td>
									<a href={ templ.SafeURL(fmt.Sprintf("/anime/%d", entry.AnimeID)) }>
										<img src={ entry.ImageUrl } alt={ entry.DisplayTitle() } class="thumb" loading="lazy"/>
									</a>
								</td>
								<td class="title-cell">
									<a href={ templ.SafeURL(fmt.Sprintf("/anime/%d", entry.AnimeID)) }>
										{ entry.DisplayTitle() }
									</a>
								</td>
								<td class="actions-cell">
									<button
										class="remove-link"
										hx-delete={ string(templ.URL(fmt.Sprintf("/api/watchlist/%d?from=watchlist", entry.AnimeID))) }
										hx-target={ fmt.Sprintf("#watchlist-entry-%d", entry.AnimeID) }
										hx-swap="delete"
										style="background: none; border: none; cursor: pointer;"
									>Remove</button>
								</td>
							</tr>
						}
					</tbody>
				</table>
			}
		}
	}
}

func viewClass(active bool) string {
	if active {
		return "active"
	}
	return ""
}

func tabClass(active bool) string {
	if active {
		return "active"
	}
	return ""
}
