import 'package:equatable/equatable.dart';
import 'package:krow_domain/krow_domain.dart';

enum OneTimeOrderStatus { initial, loading, success, failure }

class OneTimeOrderState extends Equatable {
  const OneTimeOrderState({
    required this.date,
    required this.location,
    required this.eventName,
    required this.positions,
    this.status = OneTimeOrderStatus.initial,
    this.errorMessage,
    this.vendors = const <Vendor>[],
    this.selectedVendor,
    this.hubs = const <OneTimeOrderHubOption>[],
    this.selectedHub,
    this.roles = const <OneTimeOrderRoleOption>[],
  });

  factory OneTimeOrderState.initial() {
    return OneTimeOrderState(
      date: DateTime.now(),
      location: '',
      eventName: '',
      positions: const <OneTimeOrderPosition>[
        OneTimeOrderPosition(role: '', count: 1, startTime: '', endTime: ''),
      ],
      vendors: const <Vendor>[],
      hubs: const <OneTimeOrderHubOption>[],
      roles: const <OneTimeOrderRoleOption>[],
    );
  }
  final DateTime date;
  final String location;
  final String eventName;
  final List<OneTimeOrderPosition> positions;
  final OneTimeOrderStatus status;
  final String? errorMessage;
  final List<Vendor> vendors;
  final Vendor? selectedVendor;
  final List<OneTimeOrderHubOption> hubs;
  final OneTimeOrderHubOption? selectedHub;
  final List<OneTimeOrderRoleOption> roles;

  OneTimeOrderState copyWith({
    DateTime? date,
    String? location,
    String? eventName,
    List<OneTimeOrderPosition>? positions,
    OneTimeOrderStatus? status,
    String? errorMessage,
    List<Vendor>? vendors,
    Vendor? selectedVendor,
    List<OneTimeOrderHubOption>? hubs,
    OneTimeOrderHubOption? selectedHub,
    List<OneTimeOrderRoleOption>? roles,
  }) {
    return OneTimeOrderState(
      date: date ?? this.date,
      location: location ?? this.location,
      eventName: eventName ?? this.eventName,
      positions: positions ?? this.positions,
      status: status ?? this.status,
      errorMessage: errorMessage ?? this.errorMessage,
      vendors: vendors ?? this.vendors,
      selectedVendor: selectedVendor ?? this.selectedVendor,
      hubs: hubs ?? this.hubs,
      selectedHub: selectedHub ?? this.selectedHub,
      roles: roles ?? this.roles,
    );
  }

  bool get isValid {
    return eventName.isNotEmpty &&
        selectedVendor != null &&
        selectedHub != null &&
        positions.isNotEmpty &&
        positions.every(
          (OneTimeOrderPosition p) =>
              p.role.isNotEmpty &&
              p.count > 0 &&
              p.startTime.isNotEmpty &&
              p.endTime.isNotEmpty,
        );
  }

  @override
  List<Object?> get props => <Object?>[
    date,
    location,
    eventName,
    positions,
    status,
    errorMessage,
    vendors,
    selectedVendor,
    hubs,
    selectedHub,
    roles,
  ];
}

class OneTimeOrderHubOption extends Equatable {
  const OneTimeOrderHubOption({
    required this.id,
    required this.name,
    required this.address,
    this.placeId,
    this.latitude,
    this.longitude,
    this.city,
    this.state,
    this.street,
    this.country,
    this.zipCode,
  });

  final String id;
  final String name;
  final String address;
  final String? placeId;
  final double? latitude;
  final double? longitude;
  final String? city;
  final String? state;
  final String? street;
  final String? country;
  final String? zipCode;

  @override
  List<Object?> get props => <Object?>[
    id,
    name,
    address,
    placeId,
    latitude,
    longitude,
    city,
    state,
    street,
    country,
    zipCode,
  ];
}

class OneTimeOrderRoleOption extends Equatable {
  const OneTimeOrderRoleOption({
    required this.id,
    required this.name,
    required this.costPerHour,
  });

  final String id;
  final String name;
  final double costPerHour;

  @override
  List<Object?> get props => <Object?>[id, name, costPerHour];
}
