using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Dvb.Descriptors;
using skyscraper5.Skyscraper.IO;

namespace skyscraper8.Skyscraper.Drawing
{
	public class RfSpectrumData
	{

		private RfSpectrumData()
		{
			blocks = new List<RfSpectrumDataBlock>();
		}

		public static RfSpectrumData Create()
		{
			RfSpectrumData child = new RfSpectrumData();
			return child;
		}

		private List<RfSpectrumDataBlock> blocks;

		public RfSpectrumDataBlock CreateBlock(int minFreq, int maxFreq, int step, SatelliteDeliverySystemDescriptor.PolarizationEnum polarization)
		{
			RfSpectrumDataBlock child = new RfSpectrumDataBlock(minFreq,maxFreq,step,polarization);
			blocks.Add(child);
			return child;
		}

		public void SaveTo(Stream stream)
		{
			foreach (RfSpectrumDataBlock block in blocks)
			{
				stream.WriteUInt8(0x58);
				stream.WriteInt32LE(block.MinimumFrequency);
				stream.WriteInt32LE(block.MaximumFrequency);
				stream.WriteInt32LE(block.Step);
				stream.WriteUInt8((byte)block.Polarization);

				double[] blockData = block.Data;
				for (int i = 0; i < blockData.Length; i++)
					stream.WriteFloat64LE(blockData[i]);
			}

			stream.WriteUInt8(0x4f);
		}
	}

	public class RfSpectrumDataBlock
	{

		public RfSpectrumDataBlock(int minFreq, int maxFreq, int step, SatelliteDeliverySystemDescriptor.PolarizationEnum polarization)
		{
			MinimumFrequency = minFreq;
			MaximumFrequency = maxFreq;
			Step = step;
			Polarization = polarization;
			Data = new double[Length + 1];
		}

		public SatelliteDeliverySystemDescriptor.PolarizationEnum Polarization { get; private set; }

		public int Step { get; private set; }

		public int MaximumFrequency { get; private set; }

		public int MinimumFrequency { get; private set; }

		public int Length => (MaximumFrequency - MinimumFrequency) / Step;

		public double[] Data { get; private set; }

		public void Push(int i, double rf)
		{
			i -= MinimumFrequency;
			i /= Step;
			Data[i] = rf;
		}
	}
}
