
import 'package:domain/repository/profile_repository.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';
import '../global_provider_container.dart';
import 'shopping_cart.dart';

part 'shopping_cart_service.g.dart';

/*
 * 购物车信息与状态的服务 - 全局单例
 */
@Riverpod(keepAlive: true)
class ShoppingCartService extends _$ShoppingCartService {
  late final ProfileRepository profileRepository;

  /// 静态方法获取当前单例对象
  static ShoppingCartService getInstance({WidgetRef? ref}) {
    if (ref != null) {
      return ref.watch(shoppingCartServiceProvider.notifier);
    } else {
      return globalContainer.read(shoppingCartServiceProvider.notifier);
    }
  }

  static ShoppingCart getState({WidgetRef? ref}) {
    if (ref != null) {
      return ref.watch(shoppingCartServiceProvider);
    } else {
      return globalContainer.read(shoppingCartServiceProvider);
    }
  }

  @override
  ShoppingCart build() {
    final state = ShoppingCart();
    return state;
  }


  // 根据服务商id 和商品id 添加/更新商品 到购物车（如果商品id 已经存在则更新，没有则新增）
  void addToShoppingCart(String serviceProviderId, String productId, ProductItem productItem) {
    Log.d('根据服务商id 添加商品 到购物车');
    for (var serviceProviderItem in state.serviceProviderItemList!) {
      if (serviceProviderItem.serviceProviderId == serviceProviderId) {
        for (var productItem in serviceProviderItem.productItemList!) {
          if (productItem.productId == productId) {
            productItem = productItem;
          }else {
            serviceProviderItem.productItemList!.add(productItem);
          }
        }
      }
    }

    state.copyWith(serviceProviderItemList: state.serviceProviderItemList);
  }


  // 根据服务商id 和 商品id 删除购物车（如果找到了商品则删除对应商品）
  void deleteShoppingCart(String serviceProviderId, String productId) {
    Log.d('根据服务商id 和 商品id 删除购物车');
    for (var serviceProviderItem in state.serviceProviderItemList!) {
      if (serviceProviderItem.serviceProviderId == serviceProviderId) {
        for (var productItem in serviceProviderItem.productItemList!) {
          if (productItem.productId == productId) {
            serviceProviderItem.productItemList!.remove(productItem);
          }
        }
      }
    }

    state = state.copyWith(serviceProviderItemList: state.serviceProviderItemList);
  }

  // 根据服务商id 和 商品id 更新购物车(如果找到对应商品则更新)
  void updateShoppingCart(String serviceProviderId, String productId, ProductItem productItem) {
    Log.d('根据服务商id 和 商品id 更新购物车');
    for (var serviceProviderItem in state.serviceProviderItemList!) {
      if (serviceProviderItem.serviceProviderId == serviceProviderId) {
        for (var productItem in serviceProviderItem.productItemList!) {
          if (productItem.productId == productId) {
            productItem = productItem;
          }
        }
      }
    }

    state = state.copyWith(serviceProviderItemList: state.serviceProviderItemList);
  }


  // 根据服务商id 和 商品id 获取商品
  ProductItem? getProductItem(String serviceProviderId, String productId) {
    Log.d('根据服务商id 和 商品id 获取商品');
    for (var serviceProviderItem in state.serviceProviderItemList!) {
      if (serviceProviderItem.serviceProviderId == serviceProviderId) {
        for (var productItem in serviceProviderItem.productItemList!) {
          if (productItem.productId == productId) {
            return productItem;
          }
        }
      }
    }
  }

  // 根据服务商id 来获取商品列表
  List<ProductItem>? getProductItemListByServiceProviderId(String serviceProviderId) {
    Log.d('根据服务商id获取商品列表');
    for (var serviceProviderItem in state.serviceProviderItemList!) {
      if (serviceProviderItem.serviceProviderId == serviceProviderId) {
        return serviceProviderItem.productItemList;
      }
    }
    return null;
  }
}
