//  Class to define a event entry
//
import Foundation
class Event
{

var type: C.EV_TYPE = C.EV_TYPE.FLIGHT
var start: Int = -1
var end: Int = -1
var cf: String = ""

// type == Rental || Lodging
var carrier: String = ""

// type == Flight || Train || Ride
var to: String = ""
var ident: String = ""
var gate: String = ""
var seat: String = ""

// type == Lodging
var address: String = ""
var phone: String = ""

// type == Activity
var desc: String = ""

init?(_ json: [ String: String])
{

    for (key, val) in json {
        switch key {

        case "type":
            type = type2enum(val)
		case "start":
			start = C.str2hm(val)
		case "end":
			end = C.str2hm(val)
		case "to":
			to = val
		case "cf":
			cf = val
		case "carrier":
			carrier = val
		case "ident":
			ident = val
		case "gate":
			gate = val
		case "seat":
			seat = val
		case "address":
			address = val
		case "phone":
			phone = val
		case "desc":
			desc = val

        default:
            Log.l("Event:" + key + " - unknown event entry")

        }
    }
}

init?(_ type: C.EV_TYPE,
      start: String,
      end: String,
      to: String,
      cf: String,
      carrier: String,
      ident: String,
      gate: String,
      seat: String,
      address: String,
      phone: String,
      desc: String)
{

	self.type    = type
	self.start   = C.str2hm(start)
	self.end     = C.str2hm(end)
	self.to      = to
	self.cf      = cf
	self.carrier = carrier
	self.ident   = ident
	self.gate    = gate
	self.seat    = seat
	self.address = address
	self.phone   = phone
    self.desc    = desc
}

init?(_ type: C.EV_TYPE)
{

	self.type    = type
	self.start   = -1
	self.end     = -1
	self.to      = ""
	self.cf      = ""
	self.carrier = ""
	self.ident   = ""
	self.gate    = ""
	self.seat    = ""
	self.address = ""
	self.phone   = ""
    self.desc    = ""
}

func get_lbl(_ ev: String) -> String
{

    switch (type) {

    case .FLIGHT:
        return C.lbl_flight[ev] ?? "?"
    case .RIDE:
        return C.lbl_ride[ev] ?? "?"
    case .RENTAL:
        return C.lbl_rental[ev] ?? "?"
    case .TRAIN:
        return C.lbl_train[ev] ?? "?"
    case .LODGING:
        return C.lbl_lodging[ev] ?? "?"
    case .ACTIVITY:
        return C.lbl_activity[ev] ?? "?"

    }
}

func type2enum(_ type: String) -> C.EV_TYPE
{

    for (idx, name) in C.types.enumerated() {
        if (name == type) {
            return C.EV_TYPE(rawValue: idx)! }
    }
    Log.l("type2enum:" + type + " - unknown event type")
    return C.EV_TYPE.FLIGHT
}

func enum2int(_ type: C.EV_TYPE) -> Int
{

    return type.rawValue
}

func marshall() -> [ String: String ]
{

    var map: [ String: String ] = [:]
    map["type"] = C.types[enum2int(type)]
	if start   >= 0  { map["start"] = C.hm2str(start) }
	if end     >= 0  { map["end"] = C.hm2str(end) }
	if to      != "" { map["to"] = to }
	if cf      != "" { map["cf"] = cf }
	if carrier != "" { map["carrier"] = carrier }
	if ident   != "" { map["ident"] = ident }
	if gate    != "" { map["gate"] = gate }
	if seat    != "" { map["seat"] = seat }
	if address != "" { map["address"] = address }
	if phone   != "" { map["phone"] = phone }
	if desc    != "" { map["desc"] = desc }
    return map
}

}
