import { ChangeEvent, FormEvent, useEffect, useState } from 'react';

import { ASAllInput, ASButton, ASForm, ASText } from 'components';
import { useApi } from 'utils';

import './index.scss';

import { FactsApi } from './api';

export function Facts() {
  const [factQuery, setFactQuery] = useState('');
  const [factQueryError, setFactQueryError] = useState('');
  const [fact, setFact] = useState('');

  const { fetch: fetchRandomFacts } = useApi(FactsApi.getRandomFact);
  const { fetch: fetchSearchResult } = useApi(FactsApi.fetchSearchResult);

  useEffect(() => {
    fetchRandomFacts()
      .then((response) => {
        setFact(response?.data?.value);
      })
      .catch(() => {
        setFact('Funny fact chucknorris API Failed');
      });
  }, []);

  function handleOnFactQuerySubmit(event: FormEvent) {
    event.preventDefault();

    if (factQuery) {
      fetchSearchResult({ query: factQuery })
        .then((response) => {
          let newFact = '';

          const result = response?.data?.result;
          const resultLength = result.length;
          if (resultLength) {
            const randomFactIndex = Math.floor(Math.random() * resultLength);
            newFact = result[randomFactIndex]?.value;
          } else {
            newFact = 'No Facts with given query';
          }

          setFact(newFact);
        })
        .catch(() => {
          setFact('Funny fact chucknorris API Failed');
        });
    }

    return false;
  }

  function handleOnFactQueryChange(event: ChangeEvent<HTMLInputElement>) {
    const updatedFactQuery = event?.target?.value || '';
    const updatedFactQueryLength = updatedFactQuery.length;

    setFactQuery(updatedFactQuery);

    if (updatedFactQueryLength > 10) {
      setFactQueryError('Max 10 characters');
    } else if (updatedFactQueryLength === 0) {
      setFactQueryError('Fact query required');
    } else {
      setFactQueryError('');
    }
  }

  return (
    <div className="as-facts">
      <header className="as-facts-header">
        <img
          className="as-facts-header-image"
          src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAclBMVEX///8AoP8Amv8Anf8Amf8AnP8Al//8///0+/8Aov/t+P/O6f/4/P/T7P/f8f/q9v+u2v93w/+Ly//E5f+23v+Qzv+93v+FyP86rv8ApP9lvP9zwf+o2P8rqv9Qtf9ju/+e1P9Ksv/H5v/b7P8/rP+Z0f+bMN5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CNEc2osCY9ZrdmFbb/8FBDFo04QXz7oTMliT1DBtaEXnDX7rjSYZ6bxPnziM9svhBA8McRSwRyLeo5M2E2dSaN+wiQwL6pAbgqMvHCO1oevjW8BkJgFeo+A0hYBiUXfEs0B4bfYk9bYPP+Mz8PeUPm4xb7yU6UV5acYWG22aDiqIZFNU0VWVsTuYwzsnSef64YLkRvGf+/ArgsenOWPN9i8FcN4V77sjvPowfzmz9PyBW6cJh8pxRqNnnrHHI5Gn+SeBEEQBEEQBEEQRK9KdpH2hcyKvb34ycj33ie556I++UpGdiIo4UFUCary5VtsFds0F3Fl38Li7Vd+k32hicQtEvhfZmcplMU/0Xnp6NjU3TjOkzxKxqWvyMKgSVpra9RdFUxe+47CVTr287OuiacNCadjIM7QTcE0VKOkA3dBi0aaGIZFDnT7zAqm0iukOem3fmiGlbRMEK3KRHoXfJgO3oierP7AB4Z0HvodmatF4e/Ws+CKhI+GQYNh99MEB2hf1XZYm/JsdKStgIEj9Mj5Fpib3j0QD4dDyiJH2T+UkCaYCwI5hgU8Kaii1uWh2bKgw9eAM9psoJReT9xSEiAWYi30RAaeFPIJNznNFPUOqOfCLMR0Q/XluIVQGzrK2tZ+CjigIefMoLqhWoGJSg8gdaKqK7JF+1mwKAkWvKJqUP/mpgJ93QTOC3JwBumFWTPzDDI8wgofPOne/HJPhstFZ3rSInPCmy4sG4o8RN8JZpQyCZgWfVZi0hLrg6k5QV8n7me+SPSYZui8XPiY1Rv+0TO+t/pqIxrroytxdWq6lHKDl2msn8/W0akU4Y3Tk5BKvO9oHpraZKaaiL6kP03UNPGRavAXmGXtPDYWVITLemeXOLxB3pFpdC8Ql2Mr3wH+rjpfqUp79iZakyAai/PRPuneVo1PwiRJtu+032m2WK38N3AogiAIgiAIgiAIgiAIgiAIgiAIgiAIgiCIX+QfO0xg6iPTW6gAAAAASUVORK5CYII="
        />
        <ASText className="as-facts-header-heading" tag="h1">
          Chuck Norris
        </ASText>
        <ASText className="as-facts-header-sub-text" tag="h3">
          Random Facts
        </ASText>
      </header>
      <main className="as-facts-main">
        <section className="as-facts-main-left">
          <ASText className="as-facts-main-left-label" tag="h3">
            Did you know...
          </ASText>
          <ASText className="as-facts-main-left-fact">{fact}</ASText>
        </section>
        <ASForm className="as-facts-main-form" onSubmit={handleOnFactQuerySubmit}>
          <ASAllInput
            className="as-facts-main-form-input"
            error={factQueryError}
            id="fact-query"
            inputType="input"
            label="Search for Facts"
            onChange={handleOnFactQueryChange}
          />
          <ASButton
            disabled={Boolean(!factQuery || factQueryError)}
            className="as-facts-main-form-submit"
            text="Search"
            type="submit"
          />
        </ASForm>
      </main>
      <footer className="as-facts-footer">
        <ASText className="as-facts-footer-text">Built with: https://api.chucknorris.io/</ASText>
      </footer>
    </div>
  );
}
