import { MaterialCommunityIcons } from "@expo/vector-icons";
import { createContext, useContext, useState } from "react";

export interface IBrainDump {
  id: number;
  title: string;
  description: string;
}

interface IBrainDumpContext {
  brainDumps: IBrainDump[];
  updateBrainDumpItem: (id: number, changes: Partial<IBrainDump>) => void;
  isAddingBrainDump: boolean;
  setIsAddingBrainDump: (value: boolean) => void;
  addBrainDump: (BrainDump: IBrainDump) => void;
}

const BrainDumpContext = createContext<IBrainDumpContext | undefined>(
  undefined
);

export const BrainDumpProvider: React.FC<{ children: React.ReactNode }> = ({
  children,
}) => {
  const [isAddingBrainDump, setIsAddingBrainDump] = useState<boolean>(false);
  const [brainDumps, setBrainDumps] = useState<IBrainDump[]>([
    {
      id: 0,
      title: "Favorite Weekend Activities",
      description:
        "What's something fun we can do together this weekend? Maybe a new game, a picnic, or trying out a family recipe. Everyone share one idea!",
    },
    {
      id: 1,
      title: "What’s For Dinner",
      description:
        "What’s one meal you’d love to have for dinner this week? Let’s get creative with new ideas we haven’t tried yet.",
    },
    {
      id: 2,
      title: "The Best Thing About Today",
      description:
        "What was the highlight of your day? Let’s each take a moment to share something good that happened, no matter how small.",
    },
    {
      id: 3,
      title: "A Dream Vacation Spot",
      description:
        "If we could go anywhere in the world right now, where would it be? Everyone pick one dream destination and tell us why.",
    },
    {
      id: 4,
      title: "Favorite Childhood Memory",
      description:
        "What’s a favorite memory from your childhood? Let’s take a trip down memory lane and share some of the moments that made us smile.",
    },
    {
      id: 5,
      title: "A New Family Tradition",
      description:
        "What’s one new tradition we could start as a family? Maybe a weekly movie night, a monthly game day, or a yearly family trip. Share your ideas!",
    },
  ]);

  const addBrainDump = (BrainDump: IBrainDump) => {
    setBrainDumps((prevBrainDumps) => [
      ...prevBrainDumps,
      {
        ...BrainDump,
        id: prevBrainDumps.length
          ? prevBrainDumps[prevBrainDumps.length - 1].id + 1
          : 0,
      },
    ]);
  };

  const updateBrainDumpItem = (id: number, changes: Partial<IBrainDump>) => {
    setBrainDumps((prevBrainDumps) =>
      prevBrainDumps.map((BrainDump) =>
        BrainDump.id === id ? { ...BrainDump, ...changes } : BrainDump
      )
    );
  };

  return (
    <BrainDumpContext.Provider
      value={{
        brainDumps,
        updateBrainDumpItem,
        isAddingBrainDump,
        setIsAddingBrainDump,
        addBrainDump,
      }}
    >
      {children}
    </BrainDumpContext.Provider>
  );
};

export const useBrainDumpContext = () => useContext(BrainDumpContext)!;
