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

export enum GroceryFrequency {
  Never = "Never",
  Daily = "Daily",
  Weekly = "Weekly",
  BiWeekly = "BiWeekly",
  Monthly = "Monthly",
  Quarterly = "Quarterly",
}

export interface IGrocery {
  id: number;
  title: string;
  category: GroceryCategory;
  approved: boolean;
  recurring: boolean;
  frequency: GroceryFrequency;
  bought: boolean;
}

export enum GroceryCategory {
  Fruit = "Fruit",
  Dairy = "Dairy",
  Vegetables = "Vegetables",
  Meat = "Meat",
  Poultry = "Poultry",
  Bakery = "Bakery",
  Beverages = "Beverages",
  Snacks = "Snacks",
  Household = "Household",
  PersonalCare = "Personal Care",
  Frozen = "Frozen",
}

type MaterialIconNames = keyof typeof MaterialCommunityIcons.glyphMap;
const iconMapping: { [key in GroceryCategory]: MaterialIconNames } = {
  //за сад се иконице за категорију бирају одавде
  [GroceryCategory.Fruit]: "food-apple",
  [GroceryCategory.Dairy]: "cheese",
  [GroceryCategory.Vegetables]: "carrot",
  [GroceryCategory.Meat]: "food-steak",
  [GroceryCategory.Poultry]: "food-drumstick",
  [GroceryCategory.Bakery]: "bread-slice",
  [GroceryCategory.Beverages]: "cup-water",
  [GroceryCategory.Snacks]: "candy",
  [GroceryCategory.Household]: "home",
  [GroceryCategory.PersonalCare]: "face-man-profile",
  [GroceryCategory.Frozen]: "snowflake",
};

interface IGroceryContext {
    groceries: IGrocery[];
    iconMapping: { [key in GroceryCategory]: MaterialIconNames };
    updateGroceryItem: (id: number, changes: Partial<IGrocery>) => void;
    isAddingGrocery: boolean;
    setIsAddingGrocery: (value: boolean) => void;
    addGrocery: (grocery: IGrocery) => void;
}

const GroceryContext = createContext<IGroceryContext | undefined>(undefined);

export const GroceryProvider: React.FC<{ children: React.ReactNode }> = ({
  children,
}) => {
  const [isAddingGrocery, setIsAddingGrocery] = useState<boolean>(false);
  const [groceries, setGroceries] = useState<IGrocery[]>([
    {
      id: 0,
      title: "Carrots",
      category: GroceryCategory.Vegetables,
      approved: false,
      bought: false,
      recurring: false,
      frequency: GroceryFrequency.Never,
    },
    {
      id: 1,
      title: "Steak",
      category: GroceryCategory.Meat,
      approved: true,
      bought: false,
      recurring: false,
      frequency: GroceryFrequency.Never,
    },
    {
      id: 2,
      title: "Chicken Breast",
      category: GroceryCategory.Poultry,
      approved: true,
      bought: false,
      recurring: false,
      frequency: GroceryFrequency.Never,
    },
    {
      id: 3,
      title: "Greek Yoghurt",
      category: GroceryCategory.Dairy,
      approved: false,
      bought: false,
      recurring: false,
      frequency: GroceryFrequency.Never,
    },
  ]);

  const addGrocery = (grocery: IGrocery) => {
    setGroceries((prevGroceries) => [
      ...prevGroceries,
      {
        ...grocery,
        id: prevGroceries.length ? prevGroceries[prevGroceries.length - 1].id + 1 : 0,
      },
    ]);
  };


  const updateGroceryItem = (id: number, changes: Partial<IGrocery>) => {
    setGroceries((prevGroceries) =>
      prevGroceries.map((grocery) =>
        grocery.id === id ? { ...grocery, ...changes } : grocery
      )
    );
  };

  return (
    <GroceryContext.Provider
      value={{ groceries, iconMapping, updateGroceryItem, isAddingGrocery, setIsAddingGrocery, addGrocery }}
    >
      {children}
    </GroceryContext.Provider>
  );
};

export const useGroceryContext = () => useContext(GroceryContext)!;
