﻿
// PublishFare : Represents one flight fare

PublishFare = createClass();
PublishFare.prototype.init = function(args) {
    /*
        id          : Unique fare identifier
        amt         : Base fare amount
		tax			: Fare tax
        currency	: Fare currency
        fareType    : Refundable or Non-refundable
        obFlights	: Array of outbound-flights
        ibFlights	: Array of inbound-flights
		obDuration	: Total outbound duration
		ibDuration	: Total inbound duration
		fareBreakup : Fare (base and tax) breakup for Adult, Child, Infant
    */

	defaults = { id: null, amt: 0, tax: 0, currency: "", fareType: "", 
	             obFlights: [], ibFlights: [], obDuration: null, ibDuration: null, fareBreakup: null, serviceCharge: 0, discount: 0,promodiscount:0,pricingAirline:"" };
	for(var pr in defaults)
		this[pr] = isDefined(args[pr]) ?args[pr] :defaults[pr];

	// Load details from fare json (usually downloaded from server)
	var i, flight, duration;

    if(!(args instanceof PublishFare)) {
	    this.id = args.id;
	    this.amt = args.a;
	    this.tax = args.t;
	    this.currency = args.c;
	    this.fareType = args.f;
	    this.serviceCharge = args.s;        
        this.discount = args.d;
        this.promodiscount = args.pd;
        this.pricingAirline = args.pa
	    // Load outbound flights
	    for(i=0; i<args.ob.length; i++) {
		    flight = new Flight(args.ob[i]);
		    this.obFlights.push(flight);
	    }

	    // Load inbound flights
	    for(i=0; i<args.ib.length; i++) {
		    flight = new Flight(args.ib[i]);
		    this.ibFlights.push(flight);
	    }

	    // Load total duration
	    this.obDuration = new Duration(args.odu);
	    this.ibDuration = new Duration(args.idu);
	    this.fareBreakup = new FareBreakup(args.b);
	}
	
    // Get a property/aggregated value
    this.get = function(pr, mode) {
	    var arr, value = null; 
        if(/_/.test(pr)) {
            mode = pr.split("_")[1];
            pr = pr.split("_")[0];
        }
        
	    mode = isDefined(mode) ?mode :"rt";
	    var obMode = (/ob/i.test(mode) || mode == 0) ?true :false;
        var rtMode = /rt/i.test(mode);

        if(obMode)
		    arr = this.obFlights;
	    else if(rtMode) {
	        var flights = new Array();
            var i;
            for(i=0; i<this.obFlights.length; i++)
                flights.push(this.obFlights[i]);
            for(i=0; i<this.ibFlights.length; i++)
                flights.push(this.ibFlights[i]);
            arr = flights;
        }
	    else
	        arr = this.ibFlights;
		        
	    if(/origin/i.test(pr)) {
		    // Origin airport		    
		    value = arr[0].originCode;
	    }
	    else if(/(dest)|(destination)/i.test(pr)) {
		    // Destination airport
		    value = arr[arr.length-1].destinationCode;
	    }
	    else if(/total/i.test(pr)) {
		    return Math.ceil((this.amt + this.tax - this.discount- this.promodiscount) * 100)/100;
	    }
	    else if(/flights/i.test(pr)) {
		    // Flights list
		    value = arr;
	    }
	    else if(/airlineCode$/i.test(pr)) {
		    // Airline code if all flights have same airline code, multi otherwise
		    var code = arr[0].airlineCode;
		    for(var i=1; i<arr.length; i++)
			    if(arr[i].airlineCode != code) {
				    code = "mult";
				    break;	
			    }
		    value = code;
	    }
	    else if(/airlineCodes/i.test(pr)) {
		    // Airline codes of all flights
		    var code = {};
		    for(var i=0; i<arr.length; i++)
			    if(!isDefined(code[arr[i].airlineCode]))
			        code[arr[i].airlineCode] = arr[i].airlineCode;
		    value = convertToArr(code);
	    }
	    else if (/flightNumber/i.test(pr)) {
		    // Flight-number for first flight
		    value = arr[0].airlineCode + "-" + arr[0].flightNumber;
	    }
	    else if (/dept/i.test(pr)) {
		    // Departure time for first flight
		    value = arr[0].deptTime;
		    if(/deptMin/i.test(pr)) 
		        value = (value.getHours()*60) + value.getMinutes();
	    }
	    else if (/arrv/i.test(pr)) {
		    // Arrival time for last flight
		    value = arr[arr.length-1].arrvTime;
		    if(/arrvMin/i.test(pr)) 
		        value = (value.getHours()*60) + value.getMinutes();
	    }
	    else if (/stops/i.test(pr)) {
		    // Number of stops
		    
            if(obMode || !rtMode) {
                var stops;
                if(obMode)
                    stops = this.obFlights.length-1;
                else
                    stops = (this.ibFlights.length == 0) ?0 :this.ibFlights.length-1;
		        for(var i=0; i<arr.length; i++)
		            stops += arr[i].stops;
                value = stops;
            }
            else {
                var obStops = this.get('stops', 'ob');
                var ibStops = this.get('stops', 'ib');
                value = (obStops > ibStops) ?obStops :ibStops;
            }
	    }
	    else if(/duration/i.test(pr)) {
		    // Duration of trip
		    if(obMode)
			    value = this.obDuration;
		    else if(rtMode) {
		        value = new Duration({});
		        value.add(this.obDuration);
		        value.add(this.ibDuration);
		    }
		    else
			    value = this.ibDuration;
	    }
	    else if(isDefined(this[pr])) {
		    value = this[pr];
	    }
	    return value;
    };
};

// Static functions for comparing two fares on basis of property
PublishFare.compare = {
	amt: function(f1, f2) {
		return f1.amt-f2.amt;
	},
	fareType: function(f1, f2) {
		return f1.fareType.localeCompare(f2.fareType);
	},
	dept: function(f1, f2) {
		return Util.compareTimes(f1.get("dept"), f2.get("dept"));
	},
	arrv: function(f1, f2) {
		return Util.compareTimes(f1.get("arrv"), f2.get("arrv"));
	},
	duration: function(f1, f2) {
		var m1 = f1.get('duration').get('minutes');
		var m2 = f2.get('duration').get('minutes');
		return m1 - m2;
	}
};
