//
//  ViewController.swift
//  Sojourn
//
//  Created by Donald Dugger on 4/15/26.
//

import UIKit

class EventPop: PopupLayout
{

var delegate: PopupVC?

var event: Event?

var type_inp: PopupInput!
var start_inp: PopupInput!
var end_inp: PopupInput!
var cf_inp: PopupInput!
var to_inp: PopupInput!
var carrier_inp: PopupInput!
var ident_inp: PopupInput!
var gate_inp: PopupInput!
var seat_inp: PopupInput!
var address_inp: PopupInput!
var phone_inp: PopupInput!
var desc_inp: PopupInput!

init?()
{

//Log.d("EventPop()")
}

func show(_ sender: PopupVC, data: Any?)
{

    event = data as? Event
    if (event == nil) {
        sender.TitleLbl.text = "New"
        event = Event(C.EV_TYPE.FLIGHT)
    } else {
        sender.TitleLbl.text = "Update"
        event = event!
    }
    guard let ev = event else {
        return
    }
//Log.d("EventPop:show()")
    let sv = C.popup_sv(sender)

    type_inp =    PopupInput(sv, l_str: "Type", drops: C.types, def: ev.type.rawValue) { t in
                                                self.type_chg(t) }
    start_inp =   PopupInput(sv, l_str: "Start",   def: C.hm2str(ev.start), keyb: .numbersAndPunctuation, key_del: sender)
	end_inp =     PopupInput(sv, l_str: "End",     def: C.hm2str(ev.end),   keyb: .numbersAndPunctuation, key_del: sender)
	to_inp =      PopupInput(sv, l_str: "To",      def: ev.to, key_del: sender)
	cf_inp =      PopupInput(sv, l_str: "Cf",      def: ev.cf, key_del: sender)
	carrier_inp = PopupInput(sv, l_str: "Carrier", def: ev.carrier, key_del: sender)
	ident_inp =   PopupInput(sv, l_str: "Ident",   def: ev.ident, key_del: sender)
	gate_inp =    PopupInput(sv, l_str: "Gate",    def: ev.gate, key_del: sender)
	seat_inp =    PopupInput(sv, l_str: "Seat",    def: ev.seat, key_del: sender)
	address_inp = PopupInput(sv, l_str: "Address", def: ev.address, key_del: sender)
	phone_inp =   PopupInput(sv, l_str: "Phone",   def: ev.phone, key_del: sender)
	desc_inp =    PopupInput(sv, l_str: "Description", def: ev.desc, lines: 3, key_del: sender)

    sh_fields();
}

func sh_fields()
{

    switch (C.EV_TYPE(rawValue: type_inp.get_int())) {

    case .FLIGHT:
        start_inp.show("Depart", hide: false)
        end_inp.show("Arrive", hide: false)
        to_inp.show("To", hide: false)
        cf_inp.show("CF", hide: false)
        carrier_inp.show("Airline", hide: false)
        ident_inp.show("Flight", hide: false)
        gate_inp.show("Gate", hide: false)
        seat_inp.show("Seat", hide: false)
        address_inp.show("Address", hide: true)
        phone_inp.show("Phone", hide: true)
        desc_inp.show("Description", hide: true)

    case .RIDE:
        start_inp.show("Depart", hide: false)
        end_inp.show("Arrive", hide: false)
        to_inp.show("To", hide: false)
        cf_inp.show("CF", hide: false)
        carrier_inp.show("Service", hide: false)
        ident_inp.show("License", hide: false)
        gate_inp.show("Gate", hide: true)
        seat_inp.show("Seat", hide: true)
        address_inp.show("Address", hide: true)
        phone_inp.show("Phone", hide: true)
        desc_inp.show("Description", hide: true)

    case .RENTAL:
        start_inp.show("Pickup", hide: false)
        end_inp.show("Dropoff", hide: false)
        to_inp.show("To", hide: true)
        cf_inp.show("CF", hide: false)
        carrier_inp.show("Company", hide: false)
        ident_inp.show("Flight", hide: true)
        gate_inp.show("Gate", hide: true)
        seat_inp.show("Seat", hide: true)
        address_inp.show("Address", hide: true)
        phone_inp.show("Phone", hide: true)
        desc_inp.show("Description", hide: true)

    case .TRAIN:
        start_inp.show("Depart", hide: false)
        end_inp.show("Arrive", hide: false)
        to_inp.show("To", hide: false)
        cf_inp.show("CF", hide: false)
        carrier_inp.show("Railway", hide: false)
        ident_inp.show("Train", hide: false)
        gate_inp.show("Platform", hide: false)
        seat_inp.show("Seat", hide: false)
        address_inp.show("Address", hide: true)
        phone_inp.show("Phone", hide: true)
        desc_inp.show("Description", hide: true)

    case .LODGING:
        start_inp.show("Checkin", hide: false)
        end_inp.show("Checkout", hide: false)
        to_inp.show("To", hide: true)
        cf_inp.show("CF", hide: false)
        carrier_inp.show("Company", hide: false)
        ident_inp.show("Flight", hide: true)
        gate_inp.show("Gate", hide: true)
        seat_inp.show("Room", hide: false)
        address_inp.show("Address", hide: false)
        phone_inp.show("Phone", hide: false)
        desc_inp.show("Description", hide: true)

    case .ACTIVITY:
        start_inp.show("Start", hide: false)
        end_inp.show("End", hide: false)
        to_inp.show("Where", hide: false)
        cf_inp.show("CF", hide: false)
        carrier_inp.show("Airline", hide: true)
        ident_inp.show("Flight", hide: true)
        gate_inp.show("Gate", hide: true)
        seat_inp.show("Seat", hide: true)
        address_inp.show("Address", hide: true)
        phone_inp.show("Phone", hide: true)
        desc_inp.show("Description", hide: false)

    default:
        return

    }
}

func type_chg(_ t: Int)
{

    sh_fields()
}

func update(_ handler: PopupDelegate?)
{

//Log.d("EventPop:update()")
    handler?.event_upd(old: event, new: Event(C.EV_TYPE(rawValue: Int(type_inp.get_int()))!,
                                              start:   start_inp.get_string(),
                                              end:     end_inp.get_string(),
                                              to:      to_inp.get_string(),
                                              cf:      cf_inp.get_string(),
                                              carrier: carrier_inp.get_string(),
                                              ident:   ident_inp.get_string(),
                                              gate:    gate_inp.get_string(),
                                              seat:    seat_inp.get_string(),
                                              address: address_inp.get_string(),
                                              phone:   phone_inp.get_string(),
                                              desc:    desc_inp.get_multi())!)
}

}
