using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Qualcomm_ModemRFA;

namespace QFDT_Components;

public partial class ScenarioEditing : UserControl, IComponentConnector
{
	public delegate void UpdatedScenarios(List<TxPowerAction> TxPowerActionList, List<RxPowerAction> PRx_owerActionList, List<RxPowerAction> DivRx_PowerActionList);

	public ObservableCollection<TxPowerAction> TxScenarios = new ObservableCollection<TxPowerAction>();

	public ObservableCollection<RxPowerAction> PRx_Scenarios = new ObservableCollection<RxPowerAction>();

	public ObservableCollection<RxPowerAction> DRx_Scenarios = new ObservableCollection<RxPowerAction>();

	public UpdatedScenarios updatedScenariosCB;

	public ScenarioEditing()
	{
		InitializeComponent();
	}

	public void Register_UpdatedScenarios(UpdatedScenarios me)
	{
		updatedScenariosCB = me;
	}

	public void LoadData(List<TxPowerAction> TxPowerActionList, List<RxPowerAction> PRxPowerActionList, List<RxPowerAction> DRxPowerActionList)
	{
		ClearData();
		if (TxPowerActionList != null)
		{
			TxScenarios.Clear();
			for (int i = 0; i < TxPowerActionList.Count; i++)
			{
				TxScenarios.Add(TxPowerActionList[i]);
			}
			TxScnearioGrid.DataContext = TxScenarios;
			bnSave.IsEnabled = true;
		}
		if (PRxPowerActionList != null)
		{
			PRx_Scenarios.Clear();
			for (int j = 0; j < PRxPowerActionList.Count; j++)
			{
				PRx_Scenarios.Add(PRxPowerActionList[j]);
			}
			PRxScnearioGrid.DataContext = PRx_Scenarios;
		}
		if (DRxPowerActionList != null)
		{
			DRx_Scenarios.Clear();
			for (int k = 0; k < DRxPowerActionList.Count; k++)
			{
				DRx_Scenarios.Add(DRxPowerActionList[k]);
			}
			DRxScnearioGrid.DataContext = DRx_Scenarios;
		}
	}

	public void ClearData()
	{
		TxScenarios.Clear();
		TxScnearioGrid.DataContext = TxScenarios;
		PRx_Scenarios.Clear();
		PRxScnearioGrid.DataContext = PRx_Scenarios;
		DRx_Scenarios.Clear();
		DRxScnearioGrid.DataContext = DRx_Scenarios;
	}

	private void bnClone_Click(object sender, RoutedEventArgs e)
	{
		if (bnTxClone == (Button)sender)
		{
			if (TxScnearioGrid.SelectedItems.Count > 0)
			{
				TxPowerAction txPowerAction = (TxPowerAction)TxScnearioGrid.SelectedItems[0];
				int num = TxScnearioGrid.Items.IndexOf(txPowerAction);
				TxPowerAction txPowerAction2 = txPowerAction.DeepCopy();
				txPowerAction2.Description += "_clone";
				TxScenarios.Insert(num + 1, txPowerAction2);
			}
			else
			{
				MessageBox.Show(base.Resources["SelectTx"].ToString() + " !");
			}
		}
		else if (bnPRxClone == (Button)sender)
		{
			if (PRxScnearioGrid.SelectedItems.Count > 0)
			{
				RxPowerAction rxPowerAction = (RxPowerAction)PRxScnearioGrid.SelectedItems[0];
				int num2 = PRxScnearioGrid.Items.IndexOf(rxPowerAction);
				RxPowerAction rxPowerAction2 = rxPowerAction.DeepCopy();
				rxPowerAction2.Description += "_clone";
				PRx_Scenarios.Insert(num2 + 1, rxPowerAction2);
			}
			else
			{
				MessageBox.Show(base.Resources["SelectPRx"].ToString() + "!");
			}
		}
		else if (bnDRxClone == (Button)sender)
		{
			if (DRxScnearioGrid.SelectedItems.Count > 0)
			{
				RxPowerAction rxPowerAction3 = (RxPowerAction)DRxScnearioGrid.SelectedItems[0];
				int num3 = DRxScnearioGrid.Items.IndexOf(rxPowerAction3);
				RxPowerAction rxPowerAction4 = rxPowerAction3.DeepCopy();
				rxPowerAction4.Description += "_clone";
				DRx_Scenarios.Insert(num3 + 1, rxPowerAction4);
			}
			else
			{
				MessageBox.Show(base.Resources["SelectDRx"].ToString() + "!");
			}
		}
	}

	private void bnSave_Click(object sender, RoutedEventArgs e)
	{
		if (updatedScenariosCB != null)
		{
			updatedScenariosCB(TxScenarios.ToList(), PRx_Scenarios.ToList(), DRx_Scenarios.ToList());
			bnSave.IsEnabled = false;
		}
	}

	private void TxScnearioGrid_SizeChanged(object sender, SizeChangedEventArgs e)
	{
	}

	public void SetLanguage(string culture)
	{
		ResourceDictionary resourceDictionary = new ResourceDictionary();
		try
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "\\Resources\\UI_" + culture + ".xaml", UriKind.Absolute);
		}
		catch
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "..\\Resources\\UI_en-US.xaml", UriKind.Absolute);
		}
		base.Resources.MergedDictionaries.Clear();
		base.Resources.MergedDictionaries.Add(resourceDictionary);
	}
}
