package main

// The TCI control console — bindings.
//
// Thin on purpose: the state is a snapshot the radio pushed and the setters are
// one command each. Everything interesting is in internal/cat/tci_panel.go.

import (
	"fmt"

	"hamlog/internal/cat"
)

// GetTCIPanel returns the console state. Connected=false when the active CAT
// backend is not a TCI radio, so the frontend has one thing to look at rather
// than an error to distinguish from a disconnected radio.
func (a *App) GetTCIPanel() cat.TCIPanelState {
	if a.cat == nil {
		return cat.TCIPanelState{}
	}
	st, _ := a.cat.TCIPanelState()
	return st
}

// tciDo is the shape every setter below takes.
func (a *App) tciDo(fn func(cat.TCIPanelController) error) error {
	if a.cat == nil {
		return fmt.Errorf("CAT not initialized")
	}
	return a.cat.TCIPanelDo(fn)
}

// SetTCIDrive sets the transmit drive (0-100).
func (a *App) SetTCIDrive(v int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetDrive(v) })
}

// SetTCITuneDrive sets the drive TUNE uses (0-100).
func (a *App) SetTCITuneDrive(v int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetTuneDrive(v) })
}

// SetTCIMicLevel sets the microphone gain (0-100).
func (a *App) SetTCIMicLevel(v int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetMicLevel(v) })
}

// SetTCIVolume sets the receive volume in dB (0 down to -60).
func (a *App) SetTCIVolume(db int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetVolume(db) })
}

// SetTCIMute mutes or unmutes the receiver.
func (a *App) SetTCIMute(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetMute(on) })
}

// SetTCIAGC picks the AGC speed: off, long, slow, med, fast.
func (a *App) SetTCIAGC(mode string) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetAGC(mode) })
}

// SetTCISquelch turns the squelch on or off.
func (a *App) SetTCISquelch(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetSquelch(on) })
}

// SetTCISquelchLevel sets the squelch threshold in dBm.
func (a *App) SetTCISquelchLevel(v int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetSquelchLevel(v) })
}

// SetTCINB, SetTCINR, SetTCIANF and SetTCIAPF switch the receive processing.
func (a *App) SetTCINB(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetNB(on) })
}
func (a *App) SetTCINR(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetNR(on) })
}
func (a *App) SetTCIANF(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetANF(on) })
}
func (a *App) SetTCIAPF(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetAPF(on) })
}

// SetTCIFilter sets the passband edges in Hz.
func (a *App) SetTCIFilter(lo, hi int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetFilter(lo, hi) })
}

// SetTCIRIT / SetTCIXIT switch the offsets on; the Offset calls move them.
func (a *App) SetTCIRIT(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetRIT(on) })
}
func (a *App) SetTCIXIT(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetXIT(on) })
}
func (a *App) SetTCIRITOffset(hz int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetRITOffset(hz) })
}
func (a *App) SetTCIXITOffset(hz int) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetXITOffset(hz) })
}

// SetTCILock locks the radio's VFO knob.
func (a *App) SetTCILock(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetLock(on) })
}

// SetTCITune starts or stops the tune carrier.
//
// IT TRANSMITS, and at tune_drive rather than at drive — which is why the panel
// shows those two numbers next to the button rather than hiding one of them in
// a menu.
func (a *App) SetTCITune(on bool) error {
	return a.tciDo(func(t cat.TCIPanelController) error { return t.SetTune(on) })
}
