import { useState } from 'react';

import { constructAddWordUiCommand } from 'contract.cm';
import { QRCodeSVG } from 'qrcode.react';

export const App = () => {
  const [word, setWord] = useState('');

  const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
  };

  const qrCode = constructAddWordUiCommand({ word, contractId: import.meta.env.VITE_CONTRACT_ADDRESS ?? '' });

  return (
    <div>
      <form onSubmit={onSubmit}>
        <div className="mx-auto my-10 flex max-w-2xl flex-col gap-2">
          <label htmlFor="first_name" className="mb-2 block text-sm font-medium text-gray-900 dark:text-white">
            Word
          </label>
          <input
            type="text"
            id="first_name"
            className="block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500"
            placeholder="Happiness"
            required
            value={word}
            onChange={(e) => setWord(e.target.value)}
          />
        </div>
      </form>
      <div className="flex w-full justify-center py-4">
        {word && (
          <QRCodeSVG
            onClick={() => {
              navigator.clipboard.writeText(qrCode).catch((err) => {
                console.error(err);
              });
            }}
            value={qrCode}
            size={320}
            level="L"
            className="hover:cursor-pointer"
            bgColor="transparent"
            fgColor="black"
          />
        )}
      </div>
    </div>
  );
};
