import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/domain/entities/cargo.dart';
import 'package:fcs/domain/entities/pickup.dart';
import 'package:fcs/domain/vo/radio.dart';
import 'package:fcs/pages/main/model/base_model.dart';
import 'package:logging/logging.dart';

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

  StreamSubscription<QuerySnapshot> listener;

  List<RadioGroup> radioGroups1 = [
    RadioGroup(
      text: "FCS Pickup",
      index: 1,
    ),
    RadioGroup(
      text: "Courier Pickup",
      index: 2,
    ),
    RadioGroup(
      text: "FCS Drop-off",
      index: 3,
    ),
    RadioGroup(
      text: "Courier Drop-off",
      index: 4,
    ),
  ];

  List<RadioGroup> get radioGroups {
    List<RadioGroup> radioGroups = [
      RadioGroup(
        text: "Local Pickup",
        index: 1,
      ),
      RadioGroup(
        text: "Courier Pickup",
        index: 2,
      ),
      RadioGroup(
        text: "Local Drop-off",
        index: 3,
      ),
      RadioGroup(
        text: "Courier Drop-off",
        index: 4,
      ),
    ];
    return radioGroups;
  }

  List<Shipment> get pickups {
    List<Shipment> pickups = [
      Shipment(
          id: "S200412 - 12 Apr 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: 'Pending',
          date: DateTime(2020, 5, 1),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          isCourier: true,
          radioIndex: 2,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200125 - 12 May 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: 'Confirmed',
          date: DateTime(2020, 5, 6),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200441 - 13 Apr 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: "Pickuped",
          date: DateTime(2020, 5, 9),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          radioIndex: 3,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200412 - 12 Apr 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: 'Pickuped',
          date: DateTime(2020, 5, 15),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200125 - 12 May 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: 'Pickuped',
          date: DateTime(2020, 5, 20),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200441 - 13 Apr 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: "Pickuped",
          date: DateTime(2020, 5, 21),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200441 - 10 Apr 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: "Canceled",
          date: DateTime(2020, 5, 25),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Shipment(
          id: "S200441 - 6 Apr 2020",
          userName: "Ko Kyaw Nyi",
          phoneNumber: '+959111111111',
          fromTime: '1PM',
          toTime: '3PM',
          numberOfPackage: 5,
          weight: 25,
          status: "Canceled",
          date: DateTime(2020, 5, 27),
          address: '154-19 64th Ave.\nFlushing, NY 11367',
          handlingFee: 50,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
    ];
    return pickups;
  }

  List<Shipment> pickups1 = [
    Shipment(
        id: "S200412 - 12 Apr 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: 'Pending',
        date: DateTime(2020, 5, 1),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        isCourier: true,
        radioIndex: 2,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200125 - 12 May 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: 'Confirmed',
        date: DateTime(2020, 5, 6),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200441 - 13 Apr 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: "Pickuped",
        date: DateTime(2020, 5, 9),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        radioIndex: 3,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200412 - 12 Apr 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: 'Pickuped',
        date: DateTime(2020, 5, 15),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200125 - 12 May 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: 'Pickuped',
        date: DateTime(2020, 5, 20),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200441 - 13 Apr 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: "Pickuped",
        date: DateTime(2020, 5, 21),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200441 - 10 Apr 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: "Canceled",
        date: DateTime(2020, 5, 25),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
    Shipment(
        id: "S200441 - 6 Apr 2020",
        userName: "Ko Kyaw Nyi",
        phoneNumber: '+959111111111',
        fromTime: '1PM',
        toTime: '3PM',
        numberOfPackage: 5,
        weight: 25,
        status: "Canceled",
        date: DateTime(2020, 5, 27),
        address: '154-19 64th Ave.\nFlushing, NY 11367',
        handlingFee: 50,
        cargoTypes: [
          Cargo(type: 'General Cargo', weight: 25),
          Cargo(type: 'Medicine', weight: 20),
          Cargo(type: 'Dangerous Cargo', weight: 30)
        ]),
  ];

  List<Shipment> get canceled {
    List<Shipment> _p = pickups.where((e) => e.status == "Canceled").toList()
      ..sort((e1, e2) {
        return e2.date.compareTo(e1.date);
      });
    return _p;
  }

  List<Shipment> get completed {
    return pickups.where((e) => e.status == "Pickuped").toList()
      ..sort((e1, e2) {
        return e2.date.compareTo(e1.date);
      });
  }

  List<Shipment> get upcoming {
    return pickups
        .where((e) =>
            e.status == "Pending" ||
            e.status == "Confirmed" ||
            e.status == "Processed" ||
            e.status == "Rescheduled")
        .toList()
          ..sort((e1, e2) {
            return e2.date.compareTo(e1.date);
          });
  }

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

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