using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using QRCT_modem_RF_Debug_tool;

namespace QFDT_Components;

public partial class PCF_WCN39XX_CFG : UserControl, IComponentConnector
{
	private List<WCN39xx_TxScenario> scenarioList;

	public PCF_WCN39XX_CFG()
	{
		InitializeComponent();
		scenarioList = new List<WCN39xx_TxScenario>();
		InitUI();
	}

	public void loadHTMode()
	{
		WCN39xx_cboHTMode.Items.Clear();
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboHTMode, WCN39XXStatic.htModeList);
		WCN39xx_cboHTMode.SelectedIndex = 0;
	}

	private void updateScenarioToUI()
	{
		WCN39xx_lbxScenarioList.Items.Clear();
		for (int i = 0; i < scenarioList.Count; i++)
		{
			WCN39xx_lbxScenarioList.Items.Add(scenarioList[i]);
		}
	}

	private WCN39xx_TxScenario getWCN39xxTxScenarioFromUI()
	{
		WCN39xx_TxScenario wCN39xx_TxScenario = new WCN39xx_TxScenario();
		wCN39xx_TxScenario.chan = WCN39XXStatic.parseTxScenarioParamsFromUI(WCN39xx_cboTxChan);
		wCN39xx_TxScenario.htMode = WCN39XXStatic.parseTxScenarioParamsFromUI(WCN39xx_cboHTMode);
		wCN39xx_TxScenario.txRate = WCN39XXStatic.parseTxScenarioParamsFromUI(WCN39xx_cboTxRate);
		wCN39xx_TxScenario.txPwrCtrl = WCN39XXStatic.parseTxScenarioParamsFromUI(WCN39xx_cboPwrCtrlMode);
		wCN39xx_TxScenario.dacGain = WCN39xx_txtDacGain.Text;
		wCN39xx_TxScenario.gainIndex = WCN39xx_txtGainIndex.Text;
		wCN39xx_TxScenario.paCfg = WCN39xx_txtPaCfg.Text;
		wCN39xx_TxScenario.targetPower = WCN39xx_txtTxPower.Text;
		return wCN39xx_TxScenario;
	}

	private void AddScenarioToList()
	{
		try
		{
			WCN39xx_TxScenario wCN39xxTxScenarioFromUI = getWCN39xxTxScenarioFromUI();
			if (scenarioList.Count > 0)
			{
				bool flag = false;
				for (int i = 0; i < scenarioList.Count; i++)
				{
					if (scenarioList[i].IsEqual(wCN39xxTxScenarioFromUI))
					{
						flag = true;
					}
				}
				if (!flag)
				{
					scenarioList.Add(wCN39xxTxScenarioFromUI);
				}
			}
			else
			{
				scenarioList.Add(wCN39xxTxScenarioFromUI);
			}
			updateScenarioToUI();
		}
		catch (Exception ex)
		{
			MessageBox.Show("Add Scenario fail :" + ex.Message, "QFDT", MessageBoxButton.OK);
		}
	}

	private void RemoveScenarioFromList()
	{
		try
		{
			if (WCN39xx_lbxScenarioList.SelectedItem == null)
			{
				return;
			}
			for (int i = 0; i < scenarioList.Count; i++)
			{
				if (scenarioList[i].IsEqual(WCN39xx_lbxScenarioList.SelectedItem as WCN39xx_TxScenario))
				{
					scenarioList.Remove(scenarioList[i]);
				}
			}
			updateScenarioToUI();
		}
		catch (Exception ex)
		{
			MessageBox.Show("Remove Scenario fail :" + ex.Message, "QFDT", MessageBoxButton.OK);
		}
	}

	private void btWCN39xxScenarioRemove_Click(object sender, RoutedEventArgs e)
	{
		RemoveScenarioFromList();
	}

	private void btWCN39xxScenarioAdd_Click(object sender, RoutedEventArgs e)
	{
		AddScenarioToList();
	}

	public void updateWCN39xxInfoForPCF(ref WCN39xxInfo wcn39xx)
	{
		WCN39XXStatic.updateWCN39xxInfo(ref wcn39xx, scenarioList);
	}

	public void SetupUI(WCN39xxInfo wcn39xx)
	{
		scenarioList.Clear();
		if (wcn39xx != null && wcn39xx.ScenarioArray != null && wcn39xx.ScenarioArray.Length > 0)
		{
			for (int i = 0; i < wcn39xx.ScenarioArray.Length; i++)
			{
				scenarioList.Add(wcn39xx.ScenarioArray[i].DeepCopy());
			}
		}
		updateScenarioToUI();
	}

	private void WCN39xx_lboScenarioList_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		if (WCN39xx_lbxScenarioList.SelectedItem != null)
		{
			WCN39xx_TxScenario wCN39xx_TxScenario = (WCN39xx_TxScenario)WCN39xx_lbxScenarioList.SelectedItem;
			WCN39XXStatic.SetSelectedItem(WCN39xx_cboHTMode, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxScenario.htMode));
			WCN39XXStatic.SetSelectedItem(WCN39xx_cboTxChan, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxScenario.chan));
			WCN39XXStatic.SetSelectedItem(WCN39xx_cboTxRate, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxScenario.txRate));
			WCN39XXStatic.SetSelectedItem(WCN39xx_cboPwrCtrlMode, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxScenario.txPwrCtrl));
			if (wCN39xx_TxScenario.txPwrCtrl.show == "ForcedDesiredGain")
			{
				WCN39xx_txtDacGain.Text = wCN39xx_TxScenario.dacGain;
				WCN39xx_txtGainIndex.Text = wCN39xx_TxScenario.gainIndex;
				WCN39xx_txtPaCfg.Text = wCN39xx_TxScenario.paCfg;
			}
			else
			{
				WCN39xx_txtTxPower.Text = wCN39xx_TxScenario.targetPower;
			}
		}
	}

	public void loadChannel1()
	{
		WCN39xx_cboTxChan.Items.Clear();
		if (WCN39xx_cboHTMode.SelectedItem != null)
		{
			obJectForComboBox obJectForComboBox2 = (obJectForComboBox)WCN39xx_cboHTMode.SelectedItem;
			if (obJectForComboBox2.showContent.StartsWith("DSRC"))
			{
				WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxChan, WCN39XXStatic.channelNumbers11p);
			}
			else
			{
				WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxChan, WCN39XXStatic.channelNumbers);
			}
		}
		else
		{
			WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxChan, WCN39XXStatic.channelNumbers);
		}
	}

	public void InitUI()
	{
		WCN39XXStatic.RefreshComboBox(loadHTMode, WCN39xx_cboHTMode);
		WCN39xx_cboHTMode.SelectedIndex = 1;
		WCN39XXStatic.RefreshComboBox(LoadTxPwrCtrl, WCN39xx_cboPwrCtrlMode);
		DefaultPwrCtrlTarget();
		WCN39XXStatic.RefreshComboBox(loadChannel1, WCN39xx_cboTxChan);
		WCN39XXStatic.RefreshComboBox(LoadTxRate, WCN39xx_cboTxRate);
	}

	private void DefaultPwrCtrlTarget()
	{
		WCN39xx_txtTxPower.Text = "20";
		WCN39xx_txtDacGain.Text = "0";
		WCN39xx_txtGainIndex.Text = "9";
		WCN39xx_txtPaCfg.Text = "0";
	}

	private void updateWCN39xxPwrCtrlTargetVisibility()
	{
		if (WCN39xx_cboPwrCtrlMode.SelectedItem != null && (WCN39xx_cboPwrCtrlMode.SelectedItem as obJectForComboBox).showContent == "ForcedDesiredGain")
		{
			WCN39xx_sForcedDesiredGain.Visibility = Visibility.Visible;
			WCN39xx_sTargetPwr.Visibility = Visibility.Collapsed;
		}
		else
		{
			WCN39xx_sForcedDesiredGain.Visibility = Visibility.Collapsed;
			WCN39xx_sTargetPwr.Visibility = Visibility.Visible;
		}
	}

	private void LoadTxPwrCtrl()
	{
		WCN39xx_cboPwrCtrlMode.Items.Clear();
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboPwrCtrlMode, WCN39XXStatic.txPwrCtrlList);
	}

	private void WCN39xx_cboTxPwrCtrl_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		updateWCN39xxPwrCtrlTargetVisibility();
	}

	private void WCN39xx_cboHTMode_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		WCN39XXStatic.RefreshComboBox(loadChannel1, WCN39xx_cboTxChan);
		WCN39XXStatic.RefreshComboBox(LoadTxRate, WCN39xx_cboTxRate);
	}

	private void WCN39xx_cboTxCh_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		WCN39XXStatic.RefreshComboBox(LoadTxRate, WCN39xx_cboTxRate);
	}

	private void LoadTxRateCCK_5m10m()
	{
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxRate, WCN39XXStatic.TxRateCCK_5m10m);
	}

	private void LoadTxRateCCK()
	{
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxRate, WCN39XXStatic.TxRateCCK);
		LoadTxRateCCK_5m10m();
	}

	private void LoadTxRateNoHT_5m10m()
	{
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxRate, WCN39XXStatic.TxRateNoHT_5m10m);
	}

	private void LoadTxRateOFDMLegacy()
	{
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxRate, WCN39XXStatic.TxRateOFDMLegacy);
		LoadTxRateNoHT_5m10m();
	}

	private void LoadTxRateDSRC5()
	{
		WCN39XXStatic.AddItemToComboBox(WCN39xx_cboTxRate, WCN39XXStatic.TxRateDSRC5);
	}

	private void LoadTxRate()
	{
		WCN39xx_cboTxRate.Items.Clear();
		obJectForComboBox obJectForComboBox2 = ((WCN39xx_cboHTMode.SelectedItem != null) ? ((obJectForComboBox)WCN39xx_cboHTMode.SelectedItem) : null);
		if (WCN39xx_cboTxChan.SelectedItem != null)
		{
			_ = (obJectForComboBox)WCN39xx_cboTxChan.SelectedItem;
		}
		if (obJectForComboBox2.showContent == "No HT")
		{
			LoadTxRateOFDMLegacy();
		}
		else if (obJectForComboBox2.showContent == "CCK")
		{
			LoadTxRateCCK();
		}
		else if (obJectForComboBox2.showContent == "DSRC5")
		{
			LoadTxRateDSRC5();
		}
	}
}
