import 'package:byod/helper/helper.dart';
import 'package:byod/models/order_variants.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class OrderVariantValueBloc extends Cubit<List<OrderVariant>> {
  OrderVariantValueBloc() : super([]);

  void setOrderVariant(String categoryId, String id, String name, int price,
      int type, List<OrderVariant> orderVariants, String caption,
      {forChecBox = false}) {
    List<OrderVariant> orderVariantResult = [];

    if (type == typeMandatory) {
      if (orderVariants.isNotEmpty) {
        // print("category yang masuk");
        // print(categoryId);

        // hapus dulu kalau categorynya sama lalu append lagi
        for (int d = 0; d < orderVariants.length; d++) {
          // print("category yang ada");
          // print(orderVariants[d].categoryId);
          if (categoryId == orderVariants[d].categoryId) {
            orderVariants.removeAt(d);
          }
        }
        for (int z = 0; z < orderVariants.length; z++) {
          var x = {
            'categoryId': orderVariants[z].categoryId,
            'id': orderVariants[z].id,
            'price': orderVariants[z].price,
            'name': orderVariants[z].name,
            'type': type,
            'caption': orderVariants[z].caption
          };
          orderVariantResult.add(OrderVariant.createVariant(x));
        }
      }

      if (categoryId != '') {
        var x = {
          'categoryId': categoryId,
          'id': id,
          'price': price,
          'name': name,
          'type': type,
          'caption': caption
        };
        orderVariantResult.add(
          OrderVariant.createVariant(x),
        );
      }
    } else if (type == typeOptional) {
      if (orderVariants.isNotEmpty) {
        // print("category yang masuk");
        // print(categoryId);

        // hapus dulu kalau categorynya sama lalu append lagi
        for (int a = 0; a < orderVariants.length; a++) {
          // print("category yang ada");
          // print(orderVariants[d].categoryId);
          if (id == orderVariants[a].id) {
            orderVariants.removeAt(a);
          }
        }
        for (int x = 0; x < orderVariants.length; x++) {
          var l = {
            'categoryId': orderVariants[x].categoryId,
            'id': orderVariants[x].id,
            'price': orderVariants[x].price,
            'name': orderVariants[x].name,
            'type': type,
            'caption': orderVariants[x].caption
          };
          orderVariantResult.add(OrderVariant.createVariant(l));
        }
      }

      if (categoryId != '' && forChecBox == true) {
        var x = {
          'categoryId': categoryId,
          'id': id,
          'price': price,
          'name': name,
          'type': type,
          'caption': caption
        };
        orderVariantResult.add(OrderVariant.createVariant(x));
      }
    } else if (type == typeAutoInsert) {
      if (orderVariants.isNotEmpty) {
        // print("category yang masuk");
        // print(categoryId);

        for (int x = 0; x < orderVariants.length; x++) {
          var l = {
            'categoryId': orderVariants[x].categoryId,
            'id': orderVariants[x].id,
            'price': orderVariants[x].price,
            'name': orderVariants[x].name,
            'type': type,
            'caption': orderVariants[x].caption
          };
          orderVariantResult.add(OrderVariant.createVariant(l));
        }
      }
      // if (categoryId != '') {
      //   var x = {
      //     'categoryId': categoryId,
      //     'id': id,
      //     'price': price,
      //     'name': name,
      //     'type': type,
      //     'caption': caption
      //   };
      //   orderVariantResult.add(OrderVariant.createVariant(x));
      // }
    }
    // print(orderVariantResult.length);
    emit(orderVariantResult);
    // List<OrderVariant> orderVariant = [];
    // var x = {'categoryId': categoryId, 'id': id, 'name': name, 'price': price};

    // orderVariant.add(OrderVariant.createVariant(x));
    // emit(orderVariant);
  }
}
