/******************************************************************************
 *  Copyright 2015 by OLTPBenchmark Project                                   *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *    http://www.apache.org/licenses/LICENSE-2.0                              *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/


package com.oltpbenchmark.benchmarks.seats.util;

import java.sql.Timestamp;

import com.oltpbenchmark.benchmarks.seats.SEATSConstants;
import com.oltpbenchmark.util.CompositeId;

public class FlightId extends CompositeId {
    
    private static final int COMPOSITE_BITS[] = {
        14, // AIRLINE_ID
        16, // DEPART AIRPORT_ID
        16, // ARRIVE AIRPORT_ID
        16, // DEPART DATE
    };
    private static final long COMPOSITE_POWS[] = compositeBitsPreCompute(COMPOSITE_BITS);
    
    /** 
     * The airline for this flight
     */
    private int airline_id;
    /**
     * The id of the departure airport
     */
    private int depart_airport_id;
    /**
     * The id of the arrival airport
     */
    private int arrive_airport_id;
    /**
     * This is the departure time of the flight in minutes since the benchmark start date
     * @see SEATSBaseClient.getFlightTimeInMinutes()
     */
    private int depart_date;
    
    
    /**
     * Constructor
     * @param airline_id - The airline for this flight
     * @param depart_airport_id - the id of the departure airport
     * @param arrive_airport_id - the id of the arrival airport
     * @param benchmark_start - the base date of when the benchmark data starts (including past days)
     * @param flight_date - when departure date of the flight
     */
    public FlightId(long airline_id, long depart_airport_id, long arrive_airport_id, Timestamp benchmark_start, Timestamp flight_date) {
        this.airline_id = (int)airline_id;
        this.depart_airport_id = (int)depart_airport_id;
        this.arrive_airport_id = (int)arrive_airport_id;
        this.depart_date = FlightId.calculateFlightDate(benchmark_start, flight_date);
        assert(this.depart_date >= 0) : benchmark_start + " / " + flight_date;
    }
    
    public FlightId() {
        // Nothing!
    }
    
    /**
     * Constructor. Converts a composite id generated by encode() into the full object
     * @param composite_id
     */
    public FlightId(long composite_id) {
        this.set(composite_id);
    }
    
    public void set(long composite_id) {
        this.decode(composite_id);
    }
    
    @Override
    public long encode() {
        return (this.encode(COMPOSITE_BITS, COMPOSITE_POWS));
    }

    @Override
    public void decode(long composite_id) {
        long values[] = super.decode(composite_id, COMPOSITE_BITS, COMPOSITE_POWS);
        this.airline_id = (int)values[0];
        this.depart_airport_id = (int)values[1];
        this.arrive_airport_id = (int)values[2];
        this.depart_date = (int)values[3];
    }

    @Override
    public long[] toArray() {
        return (new long[]{ this.airline_id,
                            this.depart_airport_id,
                            this.arrive_airport_id,
                            this.depart_date });
    }
    
    /**
     * 
     * @param benchmark_start
     * @param flight_date
     * @return
     */
    protected static final int calculateFlightDate(Timestamp benchmark_start, Timestamp flight_date) {
        assert(benchmark_start != null);
        assert(flight_date != null);
        return (int)((flight_date.getTime() - benchmark_start.getTime()) / 3600000); // 60s * 60m * 1000
    }
    
    /**
     * @return the id
     */
    public long getAirlineId() {
        return airline_id;
    }

    /**
     * @return the depart_airport_id
     */
    public long getDepartAirportId() {
        return depart_airport_id;
    }

    /**
     * @return the arrive_airport_id
     */
    public long getArriveAirportId() {
        return arrive_airport_id;
    }

    /**
     * @return the flight departure date
     */
    public Timestamp getDepartDate(Timestamp benchmark_start) {
        return (new Timestamp(benchmark_start.getTime() + (this.depart_date * SEATSConstants.MILLISECONDS_PER_MINUTE * 60)));
    }
    
    public boolean isUpcoming(Timestamp benchmark_start, long past_days) {
        Timestamp depart_date = this.getDepartDate(benchmark_start);
        return ((depart_date.getTime() - benchmark_start.getTime()) >= (past_days * SEATSConstants.MILLISECONDS_PER_DAY)); 
    }
    
    @Override
    public String toString() {
        return String.format("FlightId{airline=%d,depart=%d,arrive=%d,date=%s}",
                             this.airline_id, this.depart_airport_id, this.arrive_airport_id, this.depart_date);
    }
    
    @Override
    public int hashCode() {
        int prime = 17;
        int result = 1;
        result = prime * result + airline_id;
        result = prime * result + depart_airport_id;
        result = prime * result + arrive_airport_id;
        result = prime * result + depart_date;
        return result;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        
        if (!(obj instanceof FlightId) || obj == null)
            return false;
             
        FlightId o = (FlightId)obj;
        return (this.airline_id == o.airline_id &&
                 this.depart_airport_id == o.depart_airport_id &&
                 this.arrive_airport_id == o.arrive_airport_id &&
                 this.depart_date == o.depart_date);
    }
}
