import { renderHeader } from '../components/header.js';
import { state } from '../state.js';
import { bytesToBase58 } from '../services/crypto-utils.js';
import { extractSeed32FromPkcs8B64 } from '../services/device-key-utils.js';
import { loadEncryptedUserSecrets } from '../services/key-vault.js';

export const pageMeta = { id: 'show-keys-view', title: 'Показать ключи' };

export function render({ navigate }) {
  const screen = document.createElement('section');
  screen.className = 'stack';

  const visible = {
    root: false,
    blockchain: false,
    device: false,
  };

  const keys = {
    root: '',
    blockchain: '',
    device: '',
  };

  screen.append(
    renderHeader({
      title: 'Показать ключи',
      leftAction: { label: '←', onClick: () => navigate('device-view') },
    }),
  );

  const card = document.createElement('div');
  card.className = 'card stack';

  const status = document.createElement('p');
  status.className = 'meta-muted';
  status.textContent = 'Загружаем сохранённые ключи...';
  card.append(status);

  const renderField = (id, label) => {
    const row = document.createElement('div');
    row.className = 'key-card stack';
    const eyeIcon = `
      <svg class="key-toggle-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
        <path d="M2.4 12s3.6-6.5 9.6-6.5S21.6 12 21.6 12s-3.6 6.5-9.6 6.5S2.4 12 2.4 12Z" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>
        <circle cx="12" cy="12" r="2.9" fill="none" stroke="currentColor" stroke-width="1.8"/>
      </svg>
    `;
    const eyeOffIcon = `
      <svg class="key-toggle-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
        <path d="M3 4l18 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
        <path d="M2.4 12s3.6-6.5 9.6-6.5c2.4 0 4.5.8 6.1 1.9" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>
        <path d="M21.6 12s-3.6 6.5-9.6 6.5c-2.4 0-4.5-.8-6.1-1.9" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round"/>
      </svg>
    `;
    row.innerHTML = `
      <div class="row">
        <span class="field-label">${label}</span>
        <button class="icon-btn key-toggle-btn" type="button" data-toggle="${id}" aria-label="Показать ключ" title="Показать ключ">${eyeOffIcon}</button>
      </div>
      <div class="key-value key-value--compact" data-value="${id}">*****</div>
    `;
    row._eyeIcon = eyeIcon;
    row._eyeOffIcon = eyeOffIcon;
    return row;
  };

  card.append(
    renderField('root', 'root key (base58)'),
    renderField('blockchain', 'blockchain.key (base58)'),
    renderField('device', 'device key (base58)'),
  );

  const setMissingState = (id) => {
    const valueEl = card.querySelector(`[data-value="${id}"]`);
    const btnEl = card.querySelector(`[data-toggle="${id}"]`);
    const field = btnEl?.closest('.key-card');
    valueEl.textContent = 'нет данных';
    btnEl.disabled = true;
    btnEl.innerHTML = field?._eyeOffIcon || '';
    btnEl.setAttribute('aria-label', 'Нет данных');
    btnEl.title = 'Нет данных';
  };

  const updateField = (id) => {
    const valueEl = card.querySelector(`[data-value="${id}"]`);
    const btnEl = card.querySelector(`[data-toggle="${id}"]`);
    const field = btnEl?.closest('.key-card');
    if (!keys[id]) {
      setMissingState(id);
      return;
    }
    valueEl.textContent = visible[id] ? keys[id] : '*****';
    btnEl.disabled = false;
    btnEl.innerHTML = visible[id]
      ? field?._eyeIcon || ''
      : field?._eyeOffIcon || '';
    btnEl.setAttribute('aria-label', visible[id] ? 'Скрыть ключ' : 'Показать ключ');
    btnEl.title = visible[id] ? 'Скрыть ключ' : 'Показать ключ';
  };

  card.querySelectorAll('[data-toggle]').forEach((button) => {
    button.addEventListener('click', () => {
      const { toggle } = button.dataset;
      if (!keys[toggle]) return;
      visible[toggle] = !visible[toggle];
      updateField(toggle);
    });
  });

  ['root', 'blockchain', 'device'].forEach((id) => updateField(id));

  const actions = document.createElement('div');
  actions.className = 'auth-footer-actions';

  const closeButton = document.createElement('button');
  closeButton.className = 'ghost-btn';
  closeButton.type = 'button';
  closeButton.textContent = 'Назад';
  closeButton.addEventListener('click', () => navigate('device-view'));
  actions.append(closeButton);

  (async () => {
    try {
      if (!state.session.login || !state.session.storagePwdInMemory) {
        throw new Error('Нет активной сессии для чтения ключей');
      }

      const savedKeys = await loadEncryptedUserSecrets(
        state.session.login,
        state.session.storagePwdInMemory,
      );

      const rootSeed32 = savedKeys.rootKey ? extractSeed32FromPkcs8B64(savedKeys.rootKey) : null;
      const blockchainSeed32 = savedKeys.blockchainKey ? extractSeed32FromPkcs8B64(savedKeys.blockchainKey) : null;
      const deviceSeed32 = savedKeys.deviceKey ? extractSeed32FromPkcs8B64(savedKeys.deviceKey) : null;

      keys.root = rootSeed32 ? bytesToBase58(rootSeed32) : '';
      keys.blockchain = blockchainSeed32 ? bytesToBase58(blockchainSeed32) : '';
      keys.device = deviceSeed32 ? bytesToBase58(deviceSeed32) : '';

      if (keys.root || keys.blockchain || keys.device) {
        status.textContent = 'Показаны только ключи, сохранённые на этом устройстве.';
      } else {
        status.textContent = 'На этом устройстве нет сохранённых ключей.';
      }
    } catch (error) {
      status.textContent = 'На этом устройстве нет сохранённых ключей.';
    }

    ['root', 'blockchain', 'device'].forEach((id) => updateField(id));
  })();

  screen.append(card, actions);
  return screen;
}
