package main

// Elecraft K3/K4 panel bindings.
//
// The controls the operator asked for and nothing else: power, volume, the
// S-meter and SWR, MOX, and an ATU tune. A K3 exposes dozens of commands, but a
// panel earns its place by covering what is reached for during a QSO — and each
// one added without a radio to test it against is a control that may or may not
// do what its label says. See internal/cat/kenwood_panel.go.

import (
	"fmt"

	"hamlog/internal/cat"
)

// GetKenwoodState returns the K3/K4 panel snapshot. Zero value when the active
// CAT backend is something else, which is how the UI knows to hide the panel.
func (a *App) GetKenwoodState() cat.KenwoodTXState {
	if a.cat == nil {
		return cat.KenwoodTXState{}
	}
	st, _ := a.cat.KenwoodState()
	return st
}

// SetKenwoodPanelMode sets the operating mode from the panel's mode row —
// CW / USB / LSB / DATA (soundcard, DT0) / RTTY (FSK D, DT2).
func (a *App) SetKenwoodPanelMode(mode string) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodPanelMode(mode) })
}

// SetKenwoodRITOffset sets the RIT/XIT offset to an absolute value in Hz.
func (a *App) SetKenwoodRITOffset(hz int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodRITOffset(hz) })
}

func (a *App) SetKenwoodPower(w int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodPower(w) })
}

func (a *App) SetKenwoodAFGain(p int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodAFGain(p) })
}

func (a *App) SetKenwoodRFGain(p int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodRFGain(p) })
}

func (a *App) SetKenwoodMicGain(p int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodMicGain(p) })
}

func (a *App) SetKenwoodSquelch(p int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodSquelch(p) })
}

func (a *App) SetKenwoodPreamp(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodPreamp(on) })
}

func (a *App) SetKenwoodAtt(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodAtt(on) })
}

func (a *App) SetKenwoodNB(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodNB(on) })
}

func (a *App) SetKenwoodNR(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodNR(on) })
}

func (a *App) SetKenwoodAGC(name string) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodAGC(name) })
}

func (a *App) SetKenwoodFilter(hz int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodFilter(hz) })
}

func (a *App) SetKenwoodAntenna(n int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodAntenna(n) })
}

func (a *App) SetKenwoodRIT(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodRIT(on) })
}

func (a *App) SetKenwoodXIT(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodXIT(on) })
}

// NudgeKenwoodRIT moves the RIT/XIT offset by delta Hz.
func (a *App) NudgeKenwoodRIT(delta int) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.NudgeKenwoodRIT(delta) })
}

// ClearKenwoodRIT zeroes the RIT and XIT offsets together, which is what the
// radio's own RC does and what an operator means by "clear it".
func (a *App) ClearKenwoodRIT() error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.ClearKenwoodRIT() })
}

// SetKenwoodTX is the panel's MOX.
func (a *App) SetKenwoodTX(on bool) error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.SetKenwoodTX(on) })
}

// ToggleKenwoodATU puts the tuner in line or bypasses it (the HOLD of the same
// front-panel switch the tune uses).
func (a *App) ToggleKenwoodATU() error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.ToggleKenwoodATU() })
}

func (a *App) TuneKenwoodATU() error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.TuneKenwoodATU() })
}

// RefreshKenwood re-reads the settings on the next poll — for when a knob was
// turned on the radio itself.
func (a *App) RefreshKenwood() error {
	return a.kenwoodPanelDo(func(k cat.KenwoodPanelController) error { return k.RefreshKenwood() })
}

func (a *App) kenwoodPanelDo(fn func(cat.KenwoodPanelController) error) error {
	if a.cat == nil {
		return fmt.Errorf("CAT not initialized")
	}
	return a.cat.KenwoodPanelDo(fn)
}
