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

import UIKit
import ConnectIQ

class WatchPop: PopupLayout {

var acts: [ String ] = [ "Action", "Send itinerary", "Start app" ]

var delegate: PopupVC?

var devs: [ IQDevice ]?

var upd_dev: IQDevice?
var upd_act: C.WATCH_ACT?

var watch_inp: PopupInput!

init?()
{

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

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

//Log.d("WatchPop:show()")
    sender.TitleLbl.text = "Garmin devs"
    sender.LeftButton.setTitle("Cancel", for: .normal)
    sender.RightButton.setTitle("Rescan", for: .normal)

    delegate = sender
    devs = data as? [ IQDevice ]

    let sv = C.popup_sv(sender)
    guard let all_devs = devs else {
        return }

    for (i, dev) in all_devs.enumerated() {
        watch_inp = PopupInput(sv, l_str: dev.friendlyName, drops: acts, def: 0) { t in
                                                self.doit(i, act_i: t) } }
}

func doit(_ idx: Int, act_i: Int)
{

    //Log.d("WatchPop:button - \(idx) => \(acts[act_i])")
    let act = C.WATCH_ACT(rawValue: act_i)
    if act != .TITLE {
        upd_dev = devs?[idx]
        upd_act = C.WATCH_ACT(rawValue: act_i)
        delegate?.OKAction(self)
    }
}

func update(_ handler: PopupDelegate?)
{

//Log.d("WatchPop:update()")
    handler?.watch_upd(upd_dev, act: upd_act)
}

}
