import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/domain/entities/box.dart';
import 'package:fcs/domain/entities/cargo.dart';
import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/domain/vo/shipment_status.dart';
import 'package:fcs/domain/vo/delivery_address.dart';
import 'package:fcs/pages/main/model/base_model.dart';
import 'package:logging/logging.dart';

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

  StreamSubscription<QuerySnapshot> listener;
  static List<ShipmentStatus> statusHistory = [
    ShipmentStatus(status: "Packed", date: DateTime(2020, 6, 1), done: true),
    ShipmentStatus(status: "Shipped", date: DateTime(2020, 6, 5), done: false),
    ShipmentStatus(
        status: "Delivered", date: DateTime(2020, 6, 15), done: false)
  ];
  static List<Package> packages = [
    // PackageModel.packages[0],
    // PackageModel.packages[1],
    // PackageModel.packages[2]
  ];

  List<Box> boxes = [
    Box(
        shipmentNumber: "A202",
        receiverNumber: "3",
        receiverName: "Ko Myo Min",
        boxNumber: "1",
        rate: 7,
        packageType: "General",
        weight: 75,
        status: "Packed",
        receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
        cargoDesc: "Clothes",
        arrivedDate: DateTime(2020, 6, 1),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        packages: packages,
        shipmentHistory: statusHistory,
        shippingAddress: DeliveryAddress(
            fullName: 'U Nyi Nyi',
            addressLine1: '154-19 64th Ave.',
            addressLine2: 'Flushing',
            city: 'NY',
            state: 'NY',
            phoneNumber: '+1 (292)215-2247'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A202",
        receiverNumber: "3",
        receiverName: "Ko Myo Min",
        boxNumber: "2",
        rate: 7,
        packageType: "General",
        weight: 75,
        status: "Packed",
        cargoDesc: "Clothes",
        arrivedDate: DateTime(2020, 6, 1),
        width: 10,
        height: 20,
        length: 30,
        shipmentWeight: 36,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'Mg Myo',
            addressLine1: '153-154 5th Thitsar.',
            addressLine2: 'South Okkalapa Township',
            city: 'Yangon',
            state: 'Myanmar',
            phoneNumber: '+09 95724 8750'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A202",
        receiverNumber: "3",
        receiverName: "Ko Myo Min",
        boxNumber: "3",
        rate: 7,
        packageType: "General",
        weight: 75,
        cargoDesc: "Shoes",
        status: "Packed",
        arrivedDate: DateTime(2020, 6, 1),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'Mg Myo',
            addressLine1: '153-154 5th Thitsar.',
            addressLine2: 'South Okkalapa Township',
            city: 'Yangon',
            state: 'Myanmar',
            phoneNumber: '+09 95724 8750'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A202",
        receiverNumber: "2",
        receiverName: "Ma Aye",
        boxNumber: "1",
        rate: 8,
        packageType: "Medicine",
        weight: 75,
        status: "Packed",
        cargoDesc: "Dietary supplement",
        arrivedDate: DateTime(2020, 6, 1),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '2 Shwe Taung Kyar St, Bahan Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'U Nyi Nyi',
            addressLine1: '154-19 64th Ave.',
            addressLine2: 'Flushing',
            city: 'NY',
            state: 'NY',
            phoneNumber: '+1 (292)215-2247'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A202",
        receiverNumber: "2",
        receiverName: "Ma Aye",
        boxNumber: "3",
        rate: 7,
        packageType: "General",
        cargoDesc: "Handbags",
        weight: 75,
        status: "Arrived",
        arrivedDate: DateTime(2020, 6, 1),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '2 Shwe Taung Kyar St, Bahan Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'U Nyi Nyi',
            addressLine1: '154-19 64th Ave.',
            addressLine2: 'Flushing',
            city: 'NY',
            state: 'NY',
            phoneNumber: '+1 (292)215-2247'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A202",
        receiverNumber: "2",
        receiverName: "Ma Aye",
        boxNumber: "2",
        rate: 7,
        packageType: "General",
        cargoDesc: "Handbags",
        weight: 75,
        status: "Shipped",
        arrivedDate: DateTime(2020, 6, 1),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '2 Shwe Taung Kyar St, Bahan Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'U Nyi Nyi',
            addressLine1: '154-19 64th Ave.',
            addressLine2: 'Flushing',
            city: 'NY',
            state: 'NY',
            phoneNumber: '+1 (292)215-2247'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A201",
        receiverNumber: "1",
        receiverName: "Ko Wai",
        boxNumber: "1",
        rate: 9,
        packageType: "Dangerous",
        cargoDesc: "Phones and Scooters",
        weight: 75,
        status: "Delivered",
        arrivedDate: DateTime(2020, 5, 21),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '3 Kambzwza St, Bahan Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'U Nyi Nyi',
            addressLine1: '154-19 64th Ave.',
            addressLine2: 'Flushing',
            city: 'NY',
            state: 'NY',
            phoneNumber: '+1 (292)215-2247'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Box(
        shipmentNumber: "A201",
        receiverNumber: "1",
        receiverName: "Ko Wai",
        boxNumber: "2",
        rate: 7,
        packageType: "General",
        cargoDesc: "Construction tools",
        weight: 75,
        status: "Delivered",
        arrivedDate: DateTime(2020, 5, 21),
        width: 10,
        height: 10,
        length: 10,
        shipmentWeight: 6,
        shipmentHistory: statusHistory,
        packages: packages,
        receiverAddress: '3 Kambzwza St, Bahan Tsp, Yangon',
        shippingAddress: DeliveryAddress(
            fullName: 'U Nyi Nyi',
            addressLine1: '154-19 64th Ave.',
            addressLine2: 'Flushing',
            city: 'NY',
            state: 'NY',
            phoneNumber: '+1 (292)215-2247'),
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
  ];

  List<Box> get completed {
    return boxes.where((e) => e.status == "Delivered").toList()
      ..sort((e1, e2) {
        return e2.packageNumber.compareTo(e1.packageNumber);
      });
  }

  List<Box> get processed {
    return boxes.where((e) => e.status == "Packed").toList()
      ..sort((e1, e2) {
        return e2.packageNumber.compareTo(e1.packageNumber);
      });
  }

  List<Box> get upcoming {
    return boxes
        .where((e) =>
            e.status == "Packed" ||
            // e.status == "Received" ||
            e.status == "Shipped" ||
            e.status == "Arrived")
        .toList()
          ..sort((e1, e2) {
            return e2.packageNumber.compareTo(e1.packageNumber);
          });
  }

  void initUser(user) {
    super.initUser(user);
  }

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