import 'dart:async';

import 'package:fcs/data/services/services.dart';
import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/domain/entities/custom_duty.dart';
import 'package:fcs/domain/entities/discount_by_weight.dart';
import 'package:fcs/domain/entities/rate.dart';
import 'package:fcs/pages/main/model/base_model.dart';
import 'package:logging/logging.dart';

class ShipmentRateModel extends BaseModel {
  final log = Logger('ShipmentRateModel');

  StreamSubscription<Rate> listener;
  Rate rate;

  void initUser(user) {
    super.initUser(user);
    if (listener != null) listener.cancel();
    listener = Services.instance.rateService.getRateStream().listen((rate) {
      this.rate = rate;
      notifyListeners();
    });
  }

  @override
  logout() async {
    if (listener != null) await listener.cancel();
  }

  // Rate

  Future<void> updateRate(Rate rate) {
    return Services.instance.rateService.updateRate(rate);
  }

  //Cargo Type

  Future<void> addCargoType(CargoType cargoType) {
    return Services.instance.rateService.createCargoType(cargoType);
  }

  Future<void> updateCargoType(CargoType cargoType) {
    return Services.instance.rateService.updateCargoType(cargoType);
  }

  Future<void> deleteCargoType(String id) {
    return Services.instance.rateService.deleteCargoType(id);
  }

  //CustomDuty

  Future<void> addCustomDuty(CustomDuty customDuty) {
    return Services.instance.rateService.createCustomDuty(customDuty);
  }

  Future<void> updateCustomDuty(CustomDuty customDuty) {
    return Services.instance.rateService.updateCustomDuty(customDuty);
  }

  Future<void> deleteCustomDuty(String id) {
    return Services.instance.rateService.deleteCustomDuty(id);
  }

  //Discount by weight
  Future<void> addDiscountByWeight(DiscountByWeight discountByWeight) {
    return Services.instance.rateService
        .createDiscountByWeight(discountByWeight);
  }

  Future<void> updateDiscountByWeight(DiscountByWeight discountByWeight) {
    return Services.instance.rateService
        .updateDiscountByWeight(discountByWeight);
  }

  Future<void> deleteDiscountByWeight(String id) {
    return Services.instance.rateService.deleteDiscountByWeight(id);
  }
}
