package com.company;

import java.util.ArrayList;

/**
 * This class is used by custome rto check the flights and their seats and reserving a seat
 * @author Seyedeh Fatemeh Ahmadzadeh
 * @version 1.0
 * @since 2019 - 3 - 5
 */

public class Customer {
    private int ID;
    private int balance;
    private Operator operator;

    public Customer(int ID , Operator operator){
        this.ID = ID;
        balance = 0;
        this.operator = operator;
    }

    /**
     * Increases the balance of the customer
     * @param balance is the amount of increased balanced
     */

    public void addBalance(int balance){
        this.balance += balance;
    }

    /**
     * Returns the ID of this customer
     * @return ID of the customer
     */

    public int getID(){
        return ID;
    }

    /**
     * Shows all available flights
     */

    public void checkFlights(){
        ArrayList<Flight> flights = operator.getFlights();
        for(Flight f : flights){
            System.out.println("date : "+f.getDate()+" time : "+f.getTime()+" destination : "+f.getDestination()+" airline : "+f.getAirline()+" airplaine kind : "+f.getAirplaineKind()+" flight number : "+f.getFlightNo());
        }
    }

    /**
     * Shows all seats and empty seats of the flight that customer has selected.
     * @param flightNo the number of flight selected by the customer
     */

    public void selectFlight(int flightNo){
        for(Flight f : operator.getFlights()){
            if(f.getFlightNo() == flightNo){
                System.out.println("all seats : ");
                for(Seat s : f.getAllseats()){
                    System.out.println("seat number : "+s.getSeatNumber()+" seat kind : "+s.getSeatKind());
                }
                System.out.println("empty seats : ");
                for(Seat s : f.getEmptyTseats()){
                    System.out.println("seat number : "+s.getSeatNumber()+" seat kind : "+s.getSeatKind());
                }
            }
        }
    }

    /**
     * Reserves a seat in the selected flight for the customer and makes a ticket
     * @param flightNo the number of selected flight
     * @param seatNo the number of selected seat
     * @param weight the weight of the customers baggage
     * @param cost the price of the ticket
     * @param food shows if the customer wants food or no
     */

    public Ticket reserve(int flightNo , int seatNo , int weight , int cost , boolean food){
        Flight flight = null;
        for(Flight f : operator.getFlights()){
            if(f.getFlightNo() == flightNo) {
                flight = f;
                break;
            }
        }
        Seat seat = null;
        for(Seat s : flight.getEmptyTseats()){
            if(s.getSeatNumber() == seatNo) {
                s.setReserveCondition("reserved");
                flight.setSeats(seatNo);
                seat = s;
                break;
            }
        }
        Ticket ticket = new Ticket(weight , cost , flight , food , seat , this);
        balance -= cost;
        return ticket;
    }

    /**
     * Returns the balance of the customer
     * @return balance of the customer
     */

    public int getBalance(){
        return balance;
    }
}
