using MySqlConnector;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.Headless;
using skyscraper8.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Dvb.Descriptors;

namespace skyscraper5.Data.MySql
{
	public partial class MySqlDataStorage : DataStorage
	{
		public bool ImportFileKnown(FileInfo fi)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				using (MySqlCommand mySqlCommand = connection.CreateCommand())
				{
					mySqlCommand.CommandText = "SELECT dateadded FROM skyscraper_imported_streams WHERE filename = @filename";
					mySqlCommand.Parameters.Add("filename", MySqlDbType.VarChar);
					mySqlCommand.Parameters["filename"].Value = fi.Name.ToLowerInvariant();
					MySqlDataReader dataReader = mySqlCommand.ExecuteReader();
					bool result = dataReader.Read();
					dataReader.Close();
					connection.Close();
					return result;
				}
			}
		}

		public void ImportMarkFileAsKnown(FileInfo fi, TimeSpan duration)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				MySqlCommand command = connection.CreateCommand();
				command.CommandText = "INSERT INTO skyscraper_imported_streams (filename, debugger, time_taken) VALUES (@filename, @debugger, @time_taken)";
				command.Parameters.AddWithValue("@filename", fi.Name.ToLowerInvariant());
				command.Parameters.AddWithValue("@debugger", Debugger.IsAttached);
				command.Parameters.AddWithValue("@time_taken", (int)duration.TotalSeconds);
				command.ExecuteNonQuery();
				connection.Close();
			}
		}


		public List<SatellitePositionEntity> UiSatellitesListAll()
		{
			List<SatellitePositionEntity> result = new List<SatellitePositionEntity>();
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				using (MySqlCommand command = connection.CreateCommand())
				{
					command.CommandText = "SELECT * FROM skyscraper_satellites";
					MySqlDataReader dataReader = command.ExecuteReader();
					while (dataReader.Read())
					{
						SatellitePositionEntity satellitePosition = new SatellitePositionEntity();
						satellitePosition.angle = dataReader.GetFloat(2);
						//satellitePosition.cardinalDirection = dataReader.GetBoolean(3) ? 0 : 1;
						satellitePosition.name = dataReader.GetString(4);
						result.Add(satellitePosition);
					}
				}
				connection.Close();
			}

			return result;
		}

		public HeadlessJob GetQueuedJob()
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				using (MySqlCommand command = connection.CreateCommand())
				{
					command.CommandText = "SELECT * FROM skyscraper_queued_jobs WHERE done = FALSE";
					MySqlDataReader mySqlDataReader = command.ExecuteReader();
					HeadlessJob result = null;
					if (mySqlDataReader.Read())
					{
						result = new HeadlessJob();
						result.uuid = Guid.Parse(mySqlDataReader.GetString(0));
						result.jobType = (HeadlessJobType)mySqlDataReader.GetInt32(2);
						if (!mySqlDataReader.IsDBNull(3))
							result.iArg1 = mySqlDataReader.GetInt32(3);
						if (!mySqlDataReader.IsDBNull(4))
							result.sArg1 = mySqlDataReader.GetString(4);
						result.isSynthetic = false;
					}

					connection.Close();
					return result;
				}
			}
		}

		public void SetQueuedJobComplete(HeadlessJob headlessJob)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				using (MySqlCommand command = connection.CreateCommand())
				{
					command.CommandText = "UPDATE skyscraper_queued_jobs SET done = TRUE WHERE uuid = @uuid";
					command.Parameters.Add("uuid", MySqlDbType.VarChar);
					command.Parameters["@uuid"].Value = headlessJob.uuid.ToString();
					command.ExecuteNonQuery();
					connection.Close();
				}
			}
		}

		public IEnumerable<Tuple<int, SatelliteDeliverySystemDescriptor.PolarizationEnum>> GetBlindscanResultFrequencies(Guid selectedGuid)
		{
			throw new NotImplementedException();
		}

		public void InsertUiBlindscanSettingHistory(int settingsWindowBlScanTunerSelection,
			int multitunerMode, int settingsWindowSetFilterTunerSelection,
			int settingsWindowDiseqc, bool settingsWindowCollectIqGraphs, bool settingsWindowCollectRfSpectrum,
			bool settingsWindowCaptureFile, int settingsWindowSatellite, int settingsWindowLnb, int dish,
			bool settingsWindowScanHorizontalLow, bool settingsWindowScanHorizontalHigh, bool settingsWindowScanVerticalLow,
			bool settingsWindowScanVerticalHigh)
		{
			throw new NotImplementedException();
		}

		public object[] GetLastUiBlindscanSettings()
		{
			throw new NotImplementedException();
		}
	}
}
