'use client';

import { useEffect, useRef, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPlus, faImage, faVolumeHigh } from '@fortawesome/free-solid-svg-icons';
import { Checkbox } from '@/components/ui/checkbox';
import type { AlertConfigItem } from '@/types/response/donation/alertConfig';
import { PER_PAGE_OPTIONS } from '@/constants/donation';

type Props = {
	items: AlertConfigItem[];
	loading: boolean;
	saving: boolean;
	checkedIDs: Set<number>;
	setCheckedIDs: React.Dispatch<React.SetStateAction<Set<number>>>;
	page: number;
	setPage: React.Dispatch<React.SetStateAction<number>>;
	perPage: number;
	setPerPage: React.Dispatch<React.SetStateAction<number>>;
	onNew: () => void;
	onEdit: (item: AlertConfigItem) => void;
	onBatchDelete: () => void;
};

export default function AlertListPanel({
	items,
	loading,
	saving,
	checkedIDs,
	setCheckedIDs,
	page,
	setPage,
	perPage,
	setPerPage,
	onNew,
	onEdit,
	onBatchDelete
}: Props) {
	const [playingAudio, setPlayingAudio] = useState<number|null>(null);
	const audioRef = useRef<HTMLAudioElement|null>(null);

	// ── 페이징 ───────────────────────────────────────
	const totalPages = Math.max(1, Math.ceil(items.length / perPage));
	const pagedItems = items.slice((page - 1) * perPage, page * perPage);

	const handlePerPageChange = (value: number) => {
		setPerPage(value);
		setPage(1);
	};

	// ── 전체선택 ─────────────────────────────────────
	const visibleIDs = pagedItems.map(i => i.id);
	const checkedCount = visibleIDs.filter(id => checkedIDs.has(id)).length;
	const allChecked = pagedItems.length > 0 && checkedCount === visibleIDs.length;
	const isIndeterminate = checkedCount > 0 && checkedCount < visibleIDs.length;

	const handleSelectAll = () => {
		setCheckedIDs(prev => {
			const next = new Set(prev);
			if (allChecked) {
				visibleIDs.forEach(id => next.delete(id));
			} else {
				visibleIDs.forEach(id => next.add(id));
			}
			return next;
		});
	};

	const handleToggleCheck = (id: number) => {
		setCheckedIDs(prev => {
			const next = new Set(prev);
			if (next.has(id)) {
				next.delete(id);
			} else {
				next.add(id);
			}
			return next;
		});
	};

	// ── 사운드 재생/정지 ─────────────────────────────
	const handlePlaySound = (itemId: number, soundUrl: string) => {
		// 이미 재생 중이면 정지
		if (playingAudio === itemId && audioRef.current) {
			audioRef.current.pause();
			audioRef.current = null;
			setPlayingAudio(null);
			return;
		}

		// 기존 재생 정지
		if (audioRef.current) {
			audioRef.current.pause();
			audioRef.current = null;
		}

		const audio = new Audio(soundUrl);
		audioRef.current = audio;
		setPlayingAudio(itemId);

		audio.play().catch(() => {});
		audio.addEventListener('ended', () => {
			setPlayingAudio(null);
			audioRef.current = null;
		});
	};

	// unmount 시 오디오 정리
	useEffect(() => {
		return () => {
			if (audioRef.current) {
				audioRef.current.pause();
				audioRef.current = null;
			}
		};
	}, []);

	return (
		<div className="alert-config__list-panel">
			<div className="alert-config__toolbar">
				<div className="alert-config__toolbar-left">
					<span className="alert-config__count">총 {items.length}개</span>
					{checkedIDs.size > 0 && (
						<span className="alert-config__count">({checkedIDs.size}개 선택)</span>
					)}
				</div>
				<div className="alert-config__toolbar-right">
					<select
						value={perPage}
						onChange={e => handlePerPageChange(Number(e.target.value))}
						className="alert-config__per-page"
						title="보여질 개수"
					>
						{PER_PAGE_OPTIONS.map(n => (
							<option key={n} value={n}>{n}개씩</option>
						))}
					</select>
					<button type="button" className="alert-config__btn" onClick={onNew}>
						<FontAwesomeIcon icon={faPlus} />
						추가
					</button>
					<button
						type="button"
						className="alert-config__btn alert-config__btn--danger"
						onClick={onBatchDelete}
						disabled={checkedIDs.size === 0 || saving}
					>
						삭제
					</button>
				</div>
			</div>

			<div className="alert-config__table-wrap">
				{loading ? (
					<div className="alert-config__empty">준비 중...</div>
				) : items.length === 0 ? (
					<div className="alert-config__empty">등록된 알림 설정이 없습니다.</div>
				) : (
					<table className="alert-config__table">
						<thead>
							<tr>
								<th className="alert-config__th--check">
									<Checkbox
										checked={isIndeterminate ? 'indeterminate' : allChecked}
										onCheckedChange={handleSelectAll}
										aria-label="전체선택"
									/>
								</th>
								<th>조건</th>
								<th>금액</th>
								<th>제목</th>
								<th>보낼 내용</th>
								<th>노출(초)</th>
								<th>활성</th>
								<th>미디어</th>
								<th>비고</th>
							</tr>
						</thead>
						<tbody>
							{pagedItems.map(item => {
								const isChecked = checkedIDs.has(item.id);
								const hasImage = item.enableImage && item.imageUrl;
								const hasSound = item.enableSound && item.soundUrl;
								return (
									<tr
										key={item.id}
										className={isChecked ? 'alert-config__row--checked' : ''}
									>
										<td className="alert-config__td--check">
											<Checkbox
												checked={isChecked}
												onCheckedChange={() => handleToggleCheck(item.id)}
												aria-label={`${item.id} 선택`}
											/>
										</td>
										<td>
											<span className={`alert-config__match-badge alert-config__match-badge--${item.matchType === 1 ? 'exact' : 'min'}`}>
												{item.matchType === 1 ? '정확히' : '이상'}
											</span>
										</td>
										<td>{item.amount.toLocaleString()}원</td>
										<td>{item.title || <span className="text-muted-foreground">미입력</span>}</td>
										<td className="alert-config__td--message">{item.message}</td>
										<td>{item.displayDurationSec}초</td>
										<td>
											<span className={`alert-config__status-badge alert-config__status-badge--${item.isActive ? 'active' : 'inactive'}`}>
												{item.isActive ? '활성' : '비활성'}
											</span>
										</td>
										<td className="alert-config__td--media">
											<div>
												{hasImage && (
													<button
														type="button"
														className="alert-config__media-btn alert-config__media-btn--image"
														title="이미지 보기"
														onClick={() => window.open(item.imageUrl!, '_blank')}
													>
														<FontAwesomeIcon icon={faImage} />
													</button>
												)}
												{hasSound && (
													<button
														type="button"
														className={`alert-config__media-btn alert-config__media-btn--sound${playingAudio === item.id ? ' alert-config__media-btn--playing' : ''}`}
														title={playingAudio === item.id ? '사운드 정지' : '사운드 재생'}
														onClick={() => handlePlaySound(item.id, item.soundUrl!)}
													>
														<FontAwesomeIcon icon={faVolumeHigh} />
													</button>
												)}
												{!hasImage && !hasSound && (
													<span className="text-muted-foreground">-</span>
												)}
											</div>
										</td>
										<td>
											<button
												type="button"
												className="alert-config__btn alert-config__btn--sm"
												onClick={() => onEdit(item)}
												disabled={isChecked}
											>
												수정
											</button>
										</td>
									</tr>
								);
							})}
						</tbody>
					</table>
				)}
			</div>

			{totalPages > 1 && (
				<div className="alert-config__pagination">
					<button
						type="button"
						className="alert-config__page-btn"
						disabled={page <= 1}
						onClick={() => setPage(p => p - 1)}
					>
						◀
					</button>
					{Array.from({ length: totalPages }, (_, i) => i + 1).map(p => (
						<button
							key={p}
							type="button"
							className={`alert-config__page-btn${p === page ? ' alert-config__page-btn--active' : ''}`}
							onClick={() => setPage(p)}
						>
							{p}
						</button>
					))}
					<button
						type="button"
						className="alert-config__page-btn"
						disabled={page >= totalPages}
						onClick={() => setPage(p => p + 1)}
					>
						▶
					</button>
				</div>
			)}
		</div>
	);
}