﻿
// DomesticFare : Represents one flight fare

DomesticFare = createClass();
DomesticFare.prototype.init = function(args) {
    /*
        id          : Unique fare identifier
        amt         : Base fare amount
		tax			: Fare tax
        currency	: Fare currency
        fareType    : Refundable or Non-refundable
        flights	    : Array of flights
		duration	: Total flight Duration
		fareBreakup : Fare (base and tax) breakup for Adult, Child, Infant
    */
	defaults = { id: null, amt: 0, tax: 0, currency: "", fareType: "r", fareBasis: "", 
	             flights: [], duration: null, fareBreakup: null, serviceCharge: 0,promodiscount:0, discount: 0 };
	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 DomesticFare)) {
	    this.id = args.id;
	    this.amt = args.a;
	    this.serviceCharge = args.s;
	    	    
	    this.tax = args.t;
	    this.currency = args.c;
	    this.fareType = args.f;
	    this.promodiscount = args.pd;
	    this.discount = args.d;
	    
        if(isDefined(args.fb))
	        this.fareBasis = args.fb;
	        
	    // Load flights
	    //for(i=0; i<args.ow.length; i++) {
	    for(i=0; i<args.ob.length; i++) {
		    flight = new Flight(args.ob[i]);
		    this.flights.push(flight);
	    }
	    // Load total duration
	    //this.duration = new Duration(args.du);
	    this.duration = new Duration(args.odu);
        this.fareBreakup = new FareBreakup(args.b);
    }
    
    this.get = function(pr) {
  	    var arr, value = null; 
  	    if(/_/.test(pr)) pr = pr.split("_")[0];
  	    
	    if(/origin/i.test(pr)) {
		    // Origin airport
		    value = this.flights[0].originCode;
	    }
	    else if(/(dest)|(destination)/i.test(pr)) {
		    // Destination airport		    
		    var arr = this.flights;
		    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 = this.flights;
	    }
	    else if(/airlineCode$/i.test(pr)) {
		    // Airline code if all flights have same airline code, multi otherwise
		    var arr = this.flights;
		    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 = {};
		    var arr = this.flights;
		    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
		    var arr = this.flights;
		    value = arr[0].airlineCode + "-" + arr[0].flightNumber;
	    }
	    else if (/dept/i.test(pr)) {
		    // Origin for first flight
		    value = this.flights[0].deptTime;
		    if(/deptMin/i.test(pr)) 
		        value = (value.getHours()*60) + value.getMinutes();
	    }	    
	    else if (/arrv/i.test(pr)) {
		    // Destination for last flight
		    arr = this.flights;
		    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
		    var arr = this.flights;
            var stops = arr.length-1;
		    for(var i=0; i<arr.length; i++)
		        stops += arr[i].stops;
		    value = stops;
	    }
	    else if(/duration/i.test(pr)) {
		    // Duration of trip
		    value = this.duration;
	    }	    
	    else if(isDefined(this[pr])) {
		    value = this[pr];
	    }
	    return value;
    };
};

// Static functions for comparing two fares on basis of property
DomesticFare.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;
	}
};
