import { FolkShape } from "./folk-shape";
import { css, html } from "./tags";
import { extractArtifactToCanvas, extractBtnCss } from "./extract-artifact";

const styles = css`
	:host {
		background: var(--rs-bg-surface, #fff);
		color: var(--rs-text-primary, #1e293b);
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
		min-width: 400px;
		min-height: 500px;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 8px 12px;
		background: linear-gradient(135deg, #f97316, #dc2626);
		color: white;
		border-radius: 8px 8px 0 0;
		font-size: 12px;
		font-weight: 600;
		cursor: move;
	}

	.header-title {
		display: flex;
		align-items: center;
		gap: 6px;
	}

	.header-actions {
		display: flex;
		gap: 4px;
	}

	.header-actions button {
		background: transparent;
		border: none;
		color: white;
		cursor: pointer;
		padding: 2px 6px;
		border-radius: 4px;
		font-size: 14px;
	}

	.header-actions button:hover {
		background: rgba(255, 255, 255, 0.2);
	}

	.content {
		display: flex;
		flex-direction: column;
		height: calc(100% - 36px);
		overflow: hidden;
	}

	.mode-tabs {
		display: flex;
		border-bottom: 1px solid #e2e8f0;
	}

	.mode-tab {
		flex: 1;
		padding: 10px;
		border: none;
		background: none;
		cursor: pointer;
		font-size: 13px;
		font-weight: 500;
		color: #64748b;
		border-bottom: 2px solid transparent;
		transition: all 0.2s;
	}

	.mode-tab:hover {
		color: #1e293b;
	}

	.mode-tab.active {
		color: #f97316;
		border-bottom-color: #f97316;
	}

	.input-area {
		padding: 12px;
		border-bottom: 1px solid #e2e8f0;
	}

	.image-upload {
		border: 2px dashed #e2e8f0;
		border-radius: 8px;
		padding: 24px;
		text-align: center;
		cursor: pointer;
		transition: all 0.2s;
	}

	.image-upload:hover {
		border-color: #f97316;
		background: #fff7ed;
	}

	.image-upload.has-image {
		padding: 8px;
	}

	.upload-icon {
		font-size: 32px;
		margin-bottom: 8px;
	}

	.upload-text {
		color: #64748b;
		font-size: 13px;
	}

	.uploaded-image {
		max-width: 100%;
		max-height: 150px;
		border-radius: 6px;
	}

	.prompt-input {
		width: 100%;
		padding: 10px 12px;
		border: 2px solid var(--rs-input-border, #e2e8f0);
		border-radius: 8px;
		font-size: 13px;
		background: var(--rs-input-bg, #fff);
		color: var(--rs-input-text, inherit);
		resize: none;
		outline: none;
		font-family: inherit;
		margin-top: 8px;
	}

	.prompt-input:focus {
		border-color: #f97316;
	}

	.controls {
		display: flex;
		gap: 8px;
		margin-top: 8px;
		align-items: center;
	}

	.duration-select, .model-select, .resolution-select, .aspect-select {
		padding: 6px 10px;
		border: 2px solid #e2e8f0;
		border-radius: 6px;
		font-size: 12px;
		background: white;
		cursor: pointer;
	}

	.model-select {
		min-width: 140px;
	}

	.seedance-options {
		display: flex;
		gap: 6px;
		margin-top: 6px;
		flex-wrap: wrap;
		align-items: center;
	}

	.seedance-options label {
		font-size: 12px;
		color: #64748b;
		display: flex;
		align-items: center;
		gap: 4px;
		cursor: pointer;
	}

	.generate-btn {
		flex: 1;
		padding: 8px 16px;
		background: linear-gradient(135deg, #f97316, #dc2626);
		color: white;
		border: none;
		border-radius: 6px;
		font-size: 13px;
		font-weight: 600;
		cursor: pointer;
		transition: opacity 0.2s;
	}

	.generate-btn:hover {
		opacity: 0.9;
	}

	.generate-btn:disabled {
		opacity: 0.5;
		cursor: not-allowed;
	}

	.video-area {
		flex: 1;
		padding: 12px;
		overflow-y: auto;
		display: flex;
		flex-direction: column;
		gap: 12px;
	}

	.placeholder {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #94a3b8;
		text-align: center;
		gap: 8px;
	}

	.placeholder-icon {
		font-size: 48px;
		opacity: 0.5;
	}

	.generated-video {
		width: 100%;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
	}

	.video-item {
		position: relative;
	}

	${extractBtnCss}

	.video-prompt {
		font-size: 11px;
		color: #64748b;
		margin-top: 4px;
		padding: 4px 8px;
		background: #f1f5f9;
		border-radius: 4px;
	}

	.loading {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 24px;
		gap: 12px;
	}

	.spinner {
		width: 32px;
		height: 32px;
		border: 3px solid #e2e8f0;
		border-top-color: #f97316;
		border-radius: 50%;
		animation: spin 1s linear infinite;
	}

	@keyframes spin {
		to { transform: rotate(360deg); }
	}

	.progress-bar {
		width: 200px;
		height: 4px;
		background: #e2e8f0;
		border-radius: 2px;
		overflow: hidden;
	}

	.progress-fill {
		height: 100%;
		background: linear-gradient(135deg, #f97316, #dc2626);
		transition: width 0.3s;
	}

	.error {
		color: #ef4444;
		padding: 12px;
		background: #fef2f2;
		border-radius: 6px;
		font-size: 13px;
	}

	.hidden-input {
		display: none;
	}
`;

export interface GeneratedVideo {
	id: string;
	prompt: string;
	url: string;
	sourceImage?: string;
	duration: number;
	timestamp: Date;
}

declare global {
	interface HTMLElementTagNameMap {
		"folk-video-gen": FolkVideoGen;
	}
}

export class FolkVideoGen extends FolkShape {
	static override tagName = "folk-video-gen";

	static override portDescriptors = [
		{ name: "prompt", type: "text" as const, direction: "input" as const },
		{ name: "image", type: "image-url" as const, direction: "input" as const },
		{ name: "video", type: "video-url" as const, direction: "output" as const },
	];

	static {
		const sheet = new CSSStyleSheet();
		const parentRules = Array.from(FolkShape.styles.cssRules)
			.map((r) => r.cssText)
			.join("\n");
		const childRules = Array.from(styles.cssRules)
			.map((r) => r.cssText)
			.join("\n");
		sheet.replaceSync(`${parentRules}\n${childRules}`);
		this.styles = sheet;
	}

	#videos: GeneratedVideo[] = [];
	#mode: "i2v" | "t2v" = "i2v";
	#sourceImage: string | null = null;
	#isLoading = false;
	#progress = 0;
	#error: string | null = null;

	#model = "seedance";
	#promptInput: HTMLTextAreaElement | null = null;
	#durationSelect: HTMLSelectElement | null = null;
	#modelSelect: HTMLSelectElement | null = null;
	#resolutionSelect: HTMLSelectElement | null = null;
	#aspectSelect: HTMLSelectElement | null = null;
	#audioToggle: HTMLInputElement | null = null;
	#seedanceOptions: HTMLElement | null = null;
	#videoArea: HTMLElement | null = null;
	#generateBtn: HTMLButtonElement | null = null;
	#imageUpload: HTMLElement | null = null;
	#fileInput: HTMLInputElement | null = null;

	get videos() {
		return this.#videos;
	}

	override createRenderRoot() {
		const root = super.createRenderRoot();

		const wrapper = document.createElement("div");
		wrapper.innerHTML = html`
			<div class="header">
				<span class="header-title">
					<span>🎬</span>
					<span>Video Gen</span>
				</span>
				<div class="header-actions">
					<button class="close-btn" title="Close">×</button>
				</div>
			</div>
			<div class="content">
				<div class="mode-tabs">
					<button class="mode-tab active" data-mode="i2v">Image to Video</button>
					<button class="mode-tab" data-mode="t2v">Text to Video</button>
				</div>
				<div class="input-area">
					<div class="image-upload">
						<div class="upload-icon">📷</div>
						<div class="upload-text">Click to upload source image</div>
					</div>
					<input type="file" class="hidden-input" accept="image/*" />
					<textarea class="prompt-input" placeholder="Describe the motion/action for the video..." rows="2"></textarea>
					<div class="controls">
						<select class="model-select">
							<option value="seedance">Seedance 2.0</option>
							<option value="seedance-fast">Seedance 2.0 Fast</option>
							<option value="kling">Kling (i2v)</option>
							<option value="wan">WAN (t2v)</option>
						</select>
						<button class="generate-btn">Generate Video</button>
					</div>
					<div class="seedance-options">
						<select class="duration-select">
							<option value="5">5s</option>
							<option value="4">4s</option>
							<option value="6">6s</option>
							<option value="8">8s</option>
							<option value="10">10s</option>
						</select>
						<select class="resolution-select">
							<option value="480p">480p</option>
							<option value="720p">720p</option>
						</select>
						<select class="aspect-select">
							<option value="16:9">16:9</option>
							<option value="9:16">9:16</option>
							<option value="1:1">1:1</option>
							<option value="4:3">4:3</option>
						</select>
						<label><input type="checkbox" class="audio-toggle" /> Audio</label>
					</div>
				</div>
				<div class="video-area">
					<div class="placeholder">
						<span class="placeholder-icon">🎬</span>
						<span>Upload an image and describe the motion</span>
					</div>
				</div>
			</div>
		`;

		// Replace the container div (slot's parent) with our wrapper
		const slot = root.querySelector("slot");
		const containerDiv = slot?.parentElement as HTMLElement;
		if (containerDiv) {
			containerDiv.replaceWith(wrapper);
		}

		this.#promptInput = wrapper.querySelector(".prompt-input");
		this.#durationSelect = wrapper.querySelector(".duration-select");
		this.#modelSelect = wrapper.querySelector(".model-select");
		this.#resolutionSelect = wrapper.querySelector(".resolution-select");
		this.#aspectSelect = wrapper.querySelector(".aspect-select");
		this.#audioToggle = wrapper.querySelector(".audio-toggle");
		this.#seedanceOptions = wrapper.querySelector(".seedance-options");
		this.#videoArea = wrapper.querySelector(".video-area");
		this.#generateBtn = wrapper.querySelector(".generate-btn");
		this.#imageUpload = wrapper.querySelector(".image-upload");
		this.#fileInput = wrapper.querySelector(".hidden-input");
		const closeBtn = wrapper.querySelector(".close-btn") as HTMLButtonElement;
		const modeTabs = wrapper.querySelectorAll(".mode-tab");

		// Mode tab switching
		modeTabs.forEach((tab) => {
			tab.addEventListener("click", (e) => {
				e.stopPropagation();
				const mode = (tab as HTMLElement).dataset.mode as "i2v" | "t2v";
				this.#setMode(mode);
				modeTabs.forEach((t) => t.classList.remove("active"));
				tab.classList.add("active");
			});
		});

		// Image upload
		this.#imageUpload?.addEventListener("click", (e) => {
			e.stopPropagation();
			this.#fileInput?.click();
		});

		this.#fileInput?.addEventListener("change", (e) => {
			const file = (e.target as HTMLInputElement).files?.[0];
			if (file) this.#handleImageUpload(file);
		});

		// Generate button
		this.#generateBtn?.addEventListener("click", (e) => {
			e.stopPropagation();
			this.#generate();
		});

		// Prevent drag on inputs
		this.#promptInput?.addEventListener("pointerdown", (e) => e.stopPropagation());

		// Extract button — delegated click
		this.#videoArea?.addEventListener("click", (e) => {
			const btn = (e.target as HTMLElement).closest(".extract-btn") as HTMLElement;
			if (!btn) return;
			e.stopPropagation();
			extractArtifactToCanvas({ url: btn.dataset.url!, mediaType: "video", title: btn.dataset.title, sourceShape: this });
		});

		// Model selector
		this.#modelSelect?.addEventListener("change", (e) => {
			e.stopPropagation();
			this.#model = this.#modelSelect?.value || "seedance";
			this.#updateModelUI(modeTabs);
		});
		this.#modelSelect?.addEventListener("pointerdown", (e) => e.stopPropagation());
		this.#resolutionSelect?.addEventListener("pointerdown", (e) => e.stopPropagation());
		this.#aspectSelect?.addEventListener("pointerdown", (e) => e.stopPropagation());
		this.#durationSelect?.addEventListener("pointerdown", (e) => e.stopPropagation());
		this.#updateModelUI(modeTabs);

		// Close button
		closeBtn.addEventListener("click", (e) => {
			e.stopPropagation();
			this.dispatchEvent(new CustomEvent("close"));
		});

		// Listen for incoming port data (arrow connections)
		this.addEventListener("port-value-changed", ((e: CustomEvent) => {
			const { name, value } = e.detail;
			if (name === "prompt" && typeof value === "string" && this.#promptInput) {
				this.#promptInput.value = value;
			} else if (name === "image" && typeof value === "string") {
				this.#sourceImage = value;
				this.#setMode("i2v");
				modeTabs.forEach((t) => {
					t.classList.toggle("active", (t as HTMLElement).dataset.mode === "i2v");
				});
				if (this.#imageUpload) {
					this.#imageUpload.classList.add("has-image");
					this.#imageUpload.innerHTML = `<img class="uploaded-image" src="${this.#escapeHtml(value)}" alt="Source" />`;
				}
			}
		}) as EventListener);

		return root;
	}

	#setMode(mode: "i2v" | "t2v") {
		this.#mode = mode;
		if (this.#imageUpload) {
			this.#imageUpload.style.display = mode === "i2v" ? "block" : "none";
		}
	}

	#updateModelUI(modeTabs: NodeListOf<Element>) {
		const isSeedance = this.#model === "seedance" || this.#model === "seedance-fast";
		if (this.#seedanceOptions) {
			this.#seedanceOptions.style.display = isSeedance ? "flex" : "none";
		}
		// WAN is t2v-only, Kling is i2v-only — auto-switch mode
		if (this.#model === "wan") {
			this.#setMode("t2v");
			modeTabs.forEach((t) => t.classList.toggle("active", (t as HTMLElement).dataset.mode === "t2v"));
		} else if (this.#model === "kling") {
			this.#setMode("i2v");
			modeTabs.forEach((t) => t.classList.toggle("active", (t as HTMLElement).dataset.mode === "i2v"));
		}
	}

	#handleImageUpload(file: File) {
		const reader = new FileReader();
		reader.onload = (e) => {
			this.#sourceImage = e.target?.result as string;
			if (this.#imageUpload) {
				this.#imageUpload.classList.add("has-image");
				this.#imageUpload.innerHTML = `<img class="uploaded-image" src="${this.#sourceImage}" alt="Source" />`;
			}
		};
		reader.readAsDataURL(file);
	}

	async #generate() {
		const prompt = this.#promptInput?.value.trim();
		if (!prompt || this.#isLoading) return;

		if (this.#mode === "i2v" && !this.#sourceImage) {
			this.#error = "Please upload a source image first";
			this.#renderError();
			return;
		}

		const duration = Number.parseInt(this.#durationSelect?.value || "4", 10);

		this.#isLoading = true;
		this.#error = null;
		this.#progress = 0;
		if (this.#generateBtn) this.#generateBtn.disabled = true;
		this.#renderLoading("Submitting...");

		try {
			const endpoint = this.#mode === "i2v" ? "/api/video-gen/i2v" : "/api/video-gen/t2v";
			const body: Record<string, any> =
				this.#mode === "i2v"
					? { image: this.#sourceImage, prompt, duration }
					: { prompt, duration };
			body.model = this.#model;
			body.duration = String(duration);
			body.resolution = this.#resolutionSelect?.value || "480p";
			body.aspect_ratio = this.#aspectSelect?.value || "16:9";
			body.generate_audio = this.#audioToggle?.checked ?? false;

			const submitRes = await fetch(endpoint, {
				method: "POST",
				headers: { "Content-Type": "application/json" },
				body: JSON.stringify(body),
			});

			if (!submitRes.ok) {
				const err = await submitRes.json().catch(() => ({}));
				throw new Error(err.error || `Generation failed: ${submitRes.statusText}`);
			}

			const submitData = await submitRes.json();

			// Poll for job completion (up to 10 minutes)
			const jobId = submitData.job_id;
			if (!jobId) throw new Error("No job ID returned");

			const deadline = Date.now() + 600_000;
			let statusMsg = "Submitting to queue...";

			while (Date.now() < deadline) {
				await new Promise((r) => setTimeout(r, 3000));

				const pollRes = await fetch(`/api/video-gen/${jobId}`);
				if (!pollRes.ok) continue;
				const pollData = await pollRes.json();

				// Update progress from real fal.ai status
				const falStatus = pollData.fal_status;
				const queuePos = pollData.queue_position;
				if (falStatus === "IN_QUEUE") {
					this.#progress = 10;
					statusMsg = queuePos != null ? `In queue (position ${queuePos})...` : "Waiting in queue...";
				} else if (falStatus === "IN_PROGRESS") {
					this.#progress = Math.min(85, this.#progress < 40 ? 40 : this.#progress + 2);
					statusMsg = "Generating video...";
				} else if (!falStatus && pollData.status === "processing") {
					this.#progress = Math.min(20, this.#progress + 2);
					statusMsg = "Starting generation...";
				}
				this.#renderLoading(statusMsg);

				if (pollData.status === "complete") {
					const video: GeneratedVideo = {
						id: crypto.randomUUID(),
						prompt,
						url: pollData.url || pollData.video_url,
						sourceImage: this.#mode === "i2v" ? this.#sourceImage || undefined : undefined,
						duration,
						timestamp: new Date(),
					};

					this.#videos.unshift(video);
					this.#renderVideos();
					this.setPortValue("video", video.url);
					this.dispatchEvent(new CustomEvent("video-generated", { detail: { video } }));
					if (this.#promptInput) this.#promptInput.value = "";
					return;
				}

				if (pollData.status === "failed") {
					throw new Error(pollData.error || "Video generation failed");
				}
			}

			throw new Error("Video generation timed out");
		} catch (error) {
			this.#error = error instanceof Error ? error.message : "Generation failed";
			this.#renderError();
		} finally {
			this.#isLoading = false;
			if (this.#generateBtn) this.#generateBtn.disabled = false;
		}
	}

	#renderLoading(message = "Generating video...") {
		if (!this.#videoArea) return;
		this.#videoArea.innerHTML = `
			<div class="loading">
				<div class="spinner"></div>
				<span>${this.#escapeHtml(message)}</span>
				<div class="progress-bar">
					<div class="progress-fill" style="width: ${this.#progress}%"></div>
				</div>
				<span style="font-size: 11px; color: #64748b;">This may take a few minutes</span>
			</div>
		`;
	}

	#renderError() {
		if (!this.#videoArea) return;
		this.#videoArea.innerHTML = `
			<div class="error">${this.#escapeHtml(this.#error || "Unknown error")}</div>
			${this.#videos.length > 0 ? this.#renderVideoList() : '<div class="placeholder"><span class="placeholder-icon">🎬</span><span>Try again</span></div>'}
		`;
	}

	#renderVideos() {
		if (!this.#videoArea) return;

		if (this.#videos.length === 0) {
			this.#videoArea.innerHTML = `
				<div class="placeholder">
					<span class="placeholder-icon">🎬</span>
					<span>Upload an image and describe the motion</span>
				</div>
			`;
			return;
		}

		this.#videoArea.innerHTML = this.#renderVideoList();
	}

	#renderVideoList(): string {
		return this.#videos
			.map(
				(vid) => `
			<div class="video-item">
				<button class="extract-btn" data-url="${this.#escapeHtml(vid.url)}" data-title="${this.#escapeHtml(vid.prompt)}" title="Extract to canvas">↗</button>
				<video class="generated-video" src="${this.#escapeHtml(vid.url)}" controls autoplay loop muted playsinline></video>
				<div class="video-prompt">${this.#escapeHtml(vid.prompt)}</div>
			</div>
		`
			)
			.join("");
	}

	#escapeHtml(text: string): string {
		const div = document.createElement("div");
		div.textContent = text;
		return div.innerHTML;
	}

	static override fromData(data: Record<string, any>): FolkVideoGen {
		const shape = FolkShape.fromData(data) as FolkVideoGen;
		return shape;
	}

	override toJSON() {
		return {
			...super.toJSON(),
			type: "folk-video-gen",
			mode: this.#mode,
			videos: this.videos.map((vid) => ({
				...vid,
				timestamp: vid.timestamp.toISOString(),
			})),
		};
	}

	override applyData(data: Record<string, any>): void {
		super.applyData(data);
	}
}
