import { state }                                        from './state.js';
import { showLoading, hideLoading, setStatus, showToast } from './ui.js';
import { renderGrid, updateSelCount, showEmptyGrid }      from './grid.js';
import * as history                                      from './history.js';

const btnRename        = document.getElementById('btn-rename');
const moveModal        = document.getElementById('move-modal');
const movePrefixInput  = document.getElementById('move-prefix');
const moveDigitsInput  = document.getElementById('move-digits');
const moveCancelBtn    = document.getElementById('move-cancel');
const moveConfirmBtn   = document.getElementById('move-confirm');
const moveErrorModal   = document.getElementById('move-error-modal');
const moveErrorCloseBtn = document.getElementById('move-error-close');

let _pendingMoveTarget = null;
let _pendingMoveFiles  = null;

moveCancelBtn.addEventListener('click', () => {
  moveModal.style.display = 'none';
  _pendingMoveTarget = null;
  _pendingMoveFiles  = null;
});

moveConfirmBtn.addEventListener('click', async () => {
  const prefix = movePrefixInput.value.trim() || 'photo';
  const digits = Math.max(1, Math.min(6, parseInt(moveDigitsInput.value) || 4));

  const newNames = _pendingMoveFiles.map((f, i) => {
    const ext = f.name.split('.').pop().toLowerCase();
    return `${prefix}_${String(i + 1).padStart(digits, '0')}.${ext}`;
  });

  const conflicts = await window.api.checkFilesExist(_pendingMoveTarget, newNames);
  if (conflicts.length) {
    moveModal.style.display = 'none';
    moveErrorModal.style.display = 'flex';
    return;
  }

  moveModal.style.display = 'none';
  const targetPath  = _pendingMoveTarget;
  const filesToMove = _pendingMoveFiles.map((f, i) => ({ ...f, newName: newNames[i] }));
  _pendingMoveTarget = null;
  _pendingMoveFiles  = null;
  await _doMoveFiles(targetPath, filesToMove);
});

moveErrorCloseBtn.addEventListener('click', () => {
  moveErrorModal.style.display = 'none';
});

export async function handleFolderDrop(targetPath, fileIndices) {
  if (!fileIndices.length || !state.currentFolder) return;
  if (targetPath === state.currentFolder) return;

  const sorted        = [...fileIndices].sort((a, b) => a - b);
  const selectedFiles = sorted.map(i => state.files[i]).filter(Boolean);
  if (!selectedFiles.length) return;

  const names     = selectedFiles.map(f => f.name);
  const conflicts = await window.api.checkFilesExist(targetPath, names);

  if (!conflicts.length) {
    await _doMoveFiles(targetPath, selectedFiles);
  } else {
    _pendingMoveTarget    = targetPath;
    _pendingMoveFiles     = selectedFiles;
    movePrefixInput.value = 'temp';
    moveModal.style.display = 'flex';
  }
}

async function _doMoveFiles(targetPath, files) {
  showLoading('파일 이동 중...');
  try {
    const result = await window.api.moveFiles({ targetFolder: targetPath, files });
    hideLoading();
    if (result.errors.length) {
      alert('일부 오류:\n' + result.errors.join('\n'));
    } else {
      showToast(`✅ ${result.moved.length}개 파일 이동 완료!`);
      setStatus(`${result.moved.length}개 파일 이동 완료`, true);
    }
    const newList = await window.api.listImages(state.currentFolder);
    state.files.length = 0;
    state.files.push(...newList);
    state.selectedIdxs.clear();
    history.reset();
    history.push(state.files);
    if (!newList.length) {
      showEmptyGrid();
      btnRename.disabled = true;
      setStatus('이미지 파일 없음', false);
    } else {
      renderGrid();
      setStatus(`${state.files.length}개 파일 로드됨`, true);
    }
  } catch (e) {
    hideLoading();
    alert('오류 발생: ' + e.message);
  }
}
