import { useState, useEffect } from 'react';
import Taro from '@tarojs/taro';

// 购物车商品接口
export interface CartItem {
  goodsId: number;
  name: string;
  price: string;
  image: string;
  quantity: number;
  addTime: number;
  skuId?: number;
  specInfo?: string;
}

// 购物车Hook
export const useCart = () => {
  const [cartItems, setCartItems] = useState<CartItem[]>([]);
  const [cartCount, setCartCount] = useState(0);

  // 从本地存储加载购物车数据
  const loadCartFromStorage = () => {
    try {
      const cartData = Taro.getStorageSync('cart_items');
      if (cartData) {
        const items = JSON.parse(cartData) as CartItem[];
        setCartItems(items);
        updateCartCount(items);
      }
    } catch (error) {
      console.error('加载购物车数据失败:', error);
    }
  };

  // 保存购物车数据到本地存储
  const saveCartToStorage = (items: CartItem[]) => {
    try {
      Taro.setStorageSync('cart_items', JSON.stringify(items));
    } catch (error) {
      console.error('保存购物车数据失败:', error);
    }
  };

  // 更新购物车数量
  const updateCartCount = (items: CartItem[]) => {
    const count = items.reduce((total, item) => total + item.quantity, 0);
    setCartCount(count);
  };

  // 添加商品到购物车
  const addToCart = (goods: {
    goodsId: number;
    name: string;
    price: string;
    image: string;
    skuId?: number;
    specInfo?: string;
  }, quantity: number = 1) => {
    const newItems = [...cartItems];
    // 如果有SKU，需要根据goodsId和skuId来判断是否为同一商品
    const existingItemIndex = newItems.findIndex(item =>
      item.goodsId === goods.goodsId &&
      (goods.skuId ? item.skuId === goods.skuId : !item.skuId)
    );

    if (existingItemIndex >= 0) {
      // 如果商品已存在，增加数量
      newItems[existingItemIndex].quantity += quantity;
    } else {
      // 如果商品不存在，添加新商品
      const newItem: CartItem = {
        goodsId: goods.goodsId,
        name: goods.name,
        price: goods.price,
        image: goods.image,
        quantity,
        addTime: Date.now(),
        skuId: goods.skuId,
        specInfo: goods.specInfo
      };
      newItems.push(newItem);
    }

    setCartItems(newItems);
    updateCartCount(newItems);
    saveCartToStorage(newItems);

    // 显示成功提示
    Taro.showToast({
      title: '加入购物车成功',
      icon: 'success',
      duration: 1500
    });
  };

  // 从购物车移除商品
  const removeFromCart = (goodsId: number) => {
    const newItems = cartItems.filter(item => item.goodsId !== goodsId);
    setCartItems(newItems);
    updateCartCount(newItems);
    saveCartToStorage(newItems);
  };

  // 更新商品数量
  const updateQuantity = (goodsId: number, quantity: number) => {
    if (quantity <= 0) {
      removeFromCart(goodsId);
      return;
    }

    const newItems = cartItems.map(item =>
      item.goodsId === goodsId ? { ...item, quantity } : item
    );
    setCartItems(newItems);
    updateCartCount(newItems);
    saveCartToStorage(newItems);
  };

  // 清空购物车
  const clearCart = () => {
    setCartItems([]);
    setCartCount(0);
    Taro.removeStorageSync('cart_items');
  };

  // 获取购物车总价
  const getTotalPrice = () => {
    return cartItems.reduce((total, item) => {
      return total + (parseFloat(item.price) * item.quantity);
    }, 0).toFixed(2);
  };

  // 检查商品是否在购物车中
  const isInCart = (goodsId: number) => {
    return cartItems.some(item => item.goodsId === goodsId);
  };

  // 获取商品在购物车中的数量
  const getItemQuantity = (goodsId: number) => {
    const item = cartItems.find(item => item.goodsId === goodsId);
    return item ? item.quantity : 0;
  };

  // 初始化时加载购物车数据
  useEffect(() => {
    loadCartFromStorage();
  }, []);

  return {
    cartItems,
    cartCount,
    addToCart,
    removeFromCart,
    updateQuantity,
    clearCart,
    getTotalPrice,
    isInCart,
    getItemQuantity,
    loadCartFromStorage
  };
};
