import 'package:syncrow_web/utils/constants/assets.dart';

import 'selected_product_model.dart';

class ProductModel {
  final String uuid;
  final String catName;
  String? name;
  final String prodId;
  final String prodType;
  String? icon;

  ProductModel({
    required this.uuid,
    required this.catName,
    required this.prodId,
    required this.prodType,
    required this.name,
    this.icon,
  });

  // Factory method to create a Product from JSON
  factory ProductModel.fromMap(Map<String, dynamic> json) {
    return ProductModel(
        uuid: json['uuid'],
        catName: json['catName'],
        prodId: json['prodId'],
        prodType: json['prodType'],
        name: json['name'] ?? '',
        icon: _mapIconToProduct(json['prodType']));
  }

  // Method to convert a Product to JSON
  Map<String, dynamic> toMap() {
    return {
      'uuid': uuid,
      'catName': catName,
      'prodId': prodId,
      'prodType': prodType,
    };
  }

  SelectedProduct toSelectedProduct(int count) {
    return SelectedProduct(
      productId: uuid,
      count: count,
      productName: name!,
      product: this,
    );
  }

  static String _mapIconToProduct(String prodType) {
    const iconMapping = {
      '1G': Assets.Gang1SwitchIcon,
      '1GT': Assets.oneTouchSwitch,
      '2G': Assets.Gang2SwitchIcon,
      '2GT': Assets.twoTouchSwitch,
      '3G': Assets.Gang3SwitchIcon,
      '3GT': Assets.threeTouchSwitch,
      'CUR': Assets.curtain,
      'CUR_2': Assets.curtain,
      'GD': Assets.garageDoor,
      'GW': Assets.SmartGatewayIcon,
      'DL': Assets.DoorLockIcon,
      'WL': Assets.waterLeakSensor,
      'WH': Assets.waterHeater,
      'WM': Assets.waterLeakSensor,
      'SOS': Assets.sos,
      'AC': Assets.ac,
      'CPS': Assets.presenceSensor,
      'PC': Assets.powerClamp,
      'WPS': Assets.presenceSensor,
      'DS': Assets.doorSensor
    };

    return iconMapping[prodType] ?? Assets.presenceSensor;
  }

  @override
  String toString() {
    return 'ProductModel(uuid: $uuid, catName: $catName, prodId: $prodId, prodType: $prodType, name: $name, icon: $icon)';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is ProductModel &&
          runtimeType == other.runtimeType &&
          uuid == other.uuid &&
          catName == other.catName &&
          prodId == other.prodId &&
          prodType == other.prodType &&
          name == other.name &&
          icon == other.icon;

  @override
  int get hashCode =>
      uuid.hashCode ^
      catName.hashCode ^
      prodId.hashCode ^
      prodType.hashCode ^
      name.hashCode ^
      icon.hashCode;
}
