import React, { useState } from 'react';
import { addRecipe } from "../services/frontendApi.js";
import { useNavigate } from "react-router-dom";
import AddBulkIngredients from "../components/AddBulkIngredients.tsx"
import AddIngredientsForm from "../components/AddIngredientsForm.tsx"
import AddStepsForm from "../components/AddStepsForm.tsx"
import AddBulkSteps from "../components/AddBulkSteps.tsx"
import { type Ingredient, type Step } from "../types/Recipe";

function AddRecipe() {
  const [newRecipeId, setNewRecipeId] = useState<number | null>(null);
  const navigate = useNavigate();
  const [ingredients, setIngredients] = useState<Ingredient[]>([]);
  const [steps, setSteps] = useState<Step[]>([]);
  const [showBulkForm, setShowBulkForm] = useState(true);

  const addRecipeForm = async (event: React.FormEvent) => {
    event.preventDefault();
    const nameElement = document.getElementById('ar-name') as HTMLInputElement | null;
    const cuisineElement = document.getElementById('ar-cuisine') as HTMLInputElement | null;
    const stepsHash = Object.fromEntries(
      steps.map(step => [step.idx, step.instructions])
    );
    if (nameElement && cuisineElement) {
      const recipeData = {
        name: nameElement.value,
        cuisine: cuisineElement.value,
        ingredients: ingredients,
        steps: stepsHash
      }
      console.log(recipeData)
      const data = await addRecipe(recipeData);
      setNewRecipeId(data.id);
    }
  };

  React.useEffect(() => {
    if (newRecipeId !== null && newRecipeId !== undefined) {
      navigate(`/recipe/${newRecipeId}`);
    }
  }, [newRecipeId, navigate]);

  return (
    <div className="add-recipe-outer">
      <form onSubmit={addRecipeForm} className="add-recipe-form">
        <input
          type="text"
          placeholder="name"
          className="ar-name"
          id="ar-name"
        />
        <input
          type="text"
          placeholder="cuisine"
          className="ar-cusine"
          id="ar-cuisine"
        />
        <button type="submit" className="ar-button">
          submit
        </button>
      </form>
      <label>
        <input
          type="checkbox"
          checked={showBulkForm}
          onChange={(e) => setShowBulkForm(e.target.checked)}
        />
        Bulk Entry
      </label>
      <div>
        {showBulkForm ?
          <AddBulkIngredients ingredients={ingredients} onChange={setIngredients} /> :
          <AddIngredientsForm ingredients={ingredients} onSubmit={setIngredients} />
        }
      </div>
      <div>
        <ul>
          {ingredients.map((ing, index) => (
            <li key={index}>{`${ing.quantity} ${ing.unit} ${ing.name}`}</li>
          ))}
        </ul>
      </div>
      <div>
        {showBulkForm ?
          <AddBulkSteps steps={steps} onChange={setSteps} /> :
          <AddStepsForm steps={steps} onSubmit={setSteps} />
        }
      </div>
      <ul>
        {steps.map((step) => (
          <li key={step.idx}>{`${step.idx}. ${step.instructions}`}</li>
        ))}
      </ul>
    </div>
  )
}
export default AddRecipe
