using MySqlConnector;
using skyscraper8.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Data.MySql
{
    public partial class MySqlDataStorage : DataStorage
    {
        private class TsdtCoordinate
        {
            public int CurrentNetworkId { get; }
            public int CurrentTransportStreamId { get; }
            public string Compliance { get; }
            public string StationIdentification { get; set; }

            public TsdtCoordinate(int currentNetworkId, int currentTransportStreamId, string compliance)
            {
                CurrentNetworkId = currentNetworkId;
                CurrentTransportStreamId = currentTransportStreamId;
                Compliance = compliance;
                StationIdentification = null;
            }

            public bool Equals(TsdtCoordinate other)
            {
                return CurrentNetworkId == other.CurrentNetworkId && CurrentTransportStreamId == other.CurrentTransportStreamId && Compliance == other.Compliance;
            }

            public override bool Equals(object obj)
            {
                return obj is TsdtCoordinate other && Equals(other);
            }

            public override int GetHashCode()
            {
                return HashCode.Combine(CurrentNetworkId, CurrentTransportStreamId, Compliance);
            }
        }

        private HashSet<TsdtCoordinate> _tsdtCoordinates;

		public bool IsCompliant(int currentNetworkId, int currentTransportStreamId, string compliance)
		{
			if (_tsdtCoordinates == null)
				_tsdtCoordinates = new HashSet<TsdtCoordinate>();

			TsdtCoordinate coordinate = new TsdtCoordinate(currentNetworkId, currentTransportStreamId, compliance);
			if (_tsdtCoordinates.Contains(coordinate))
				return true;

			using (MySqlConnection conn = new MySqlConnection(_mcsb.ToString()))
			{
				conn.Open();
				MySqlCommand command = conn.CreateCommand();
				command.CommandText = "SELECT dateadded FROM dvb_tsdt WHERE cnid = @cnid AND ctsid = @ctsid AND compliance = @compliance";
				command.Parameters.AddWithValue("@cnid", currentNetworkId);
				command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
				command.Parameters.AddWithValue("@compliance", compliance);
				MySqlDataReader dataReader = command.ExecuteReader();
				bool result = dataReader.Read();
				if (result)
					_tsdtCoordinates.Add(coordinate);
				dataReader.Close();
				conn.Close();
				return result;
			}
		}

		public void MarkAsCompliant(int currentNetworkId, int currentTransportStreamId, string compliance)
		{
			TsdtCoordinate coordinate = new TsdtCoordinate(currentNetworkId, currentTransportStreamId, compliance);
			EnqueueSpeedhack(SpeedhackType.InsertTsdt, coordinate);
			_tsdtCoordinates.Add(coordinate);
		}

		public bool SetStationIdentification(int currentNetworkId, int currentTransportStreamId, string stationIdentification)
		{
			if (_tsdtCoordinates == null)
				return false;

			TsdtCoordinate tsdtCoordinate = _tsdtCoordinates.Where(x =>
					x.CurrentNetworkId == currentNetworkId && x.CurrentTransportStreamId == currentTransportStreamId)
				.First();
			if (tsdtCoordinate.CurrentNetworkId == 0 && tsdtCoordinate.CurrentTransportStreamId == 0)
				return false;

			if (!stationIdentification.Equals(tsdtCoordinate.StationIdentification))
			{
				EnqueueSpeedhack(SpeedhackType.UpdateTsdtStationIdentification, currentNetworkId, currentTransportStreamId, stationIdentification);
				tsdtCoordinate.StationIdentification = stationIdentification;
				return true;
			}

			return false;
		}

	}
}
