using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using NpgsqlTypes;
using skyscraper5.DsmCc.Descriptors;

namespace skyscraper5.Data.PostgreSql
{
	public partial class PostgresqlDataStore
	{
		public void StoreDsmCcDoItNowEvent(DateTime value, int currentNetworkId, int currentTransportStreamId, int programNumber, StreamEventDescriptor descriptorListStreamEventDescriptor, int pid)
		{
			EnqueueTask(x => WriteDsmCcDoItNowEvent(x, value, currentNetworkId, currentTransportStreamId, programNumber, descriptorListStreamEventDescriptor, pid));
		}

		private void WriteDsmCcDoItNowEvent(NpgsqlConnection conn, DateTime value, int currentNetworkId, int currentTransportStreamId, int programNumber, StreamEventDescriptor descriptorListStreamEventDescriptor, int pid)
		{
			if (TestForDoItNowEvent(conn, value, currentNetworkId, currentTransportStreamId, programNumber, descriptorListStreamEventDescriptor, pid))
				return;

			NpgsqlCommand command = conn.CreateCommand();
			command.CommandText =
				"insert into dsmcc_do_it_now_events (cnid, ctsid, program_number, pid, utc, event_id, private_data, npt_event_microsecond, npt_event_seconds)" +
				"values (@cnid, @ctsid, @program_number, @pid, @utc, @event_id, @private_data, @npt_event_microsecond, @npt_event_seconds);";
			command.Parameters.AddWithValue("@cnid", NpgsqlDbType.Integer, currentNetworkId);
			command.Parameters.AddWithValue("@ctsid", NpgsqlDbType.Integer, currentTransportStreamId);
			command.Parameters.AddWithValue("@program_number", NpgsqlDbType.Integer, programNumber);
			command.Parameters.AddWithValue("@pid", NpgsqlDbType.Integer, pid);
			command.Parameters.AddWithValue("@utc", NpgsqlDbType.Timestamp, value);
			command.Parameters.AddWithValue("@event_id", NpgsqlDbType.Integer, (int)descriptorListStreamEventDescriptor.EventId);
			command.Parameters.AddWithValue("@private_data", NpgsqlDbType.Bytea, descriptorListStreamEventDescriptor.PrivateData);
			command.Parameters.AddWithValue("@npt_event_microsecond", NpgsqlDbType.Bigint, (long)descriptorListStreamEventDescriptor.NptEventMicroseconds);
			command.Parameters.AddWithValue("@npt_event_seconds", NpgsqlDbType.Bigint, (long)descriptorListStreamEventDescriptor.NptEventSeconds);
			command.ExecuteNonQuery();
		}

		private bool TestForDoItNowEvent(NpgsqlConnection conn, DateTime value, int currentNetworkId,
			int currentTransportStreamId, int programNumber, StreamEventDescriptor descriptorListStreamEventDescriptor,
			int pid)
		{
			NpgsqlCommand command = conn.CreateCommand();
			command.CommandText =
				"SELECT dateadded " +
				"FROM dsmcc_do_it_now_events " +
				"WHERE cnid = @cnid " +
				"AND ctsid = @ctsid " +
				"AND program_number = @program_number " +
				"AND pid = @pid " +
				"AND utc = @utc " +
				"AND event_id = @event_id";
			command.Parameters.AddWithValue("@cnid", NpgsqlDbType.Integer, currentNetworkId);
			command.Parameters.AddWithValue("@ctsid", NpgsqlDbType.Integer, currentTransportStreamId);
			command.Parameters.AddWithValue("@program_number", NpgsqlDbType.Integer, programNumber);
			command.Parameters.AddWithValue("@pid", NpgsqlDbType.Integer, pid);
			command.Parameters.AddWithValue("@utc", NpgsqlDbType.Timestamp, value);
			command.Parameters.AddWithValue("@event_id", NpgsqlDbType.Integer, (int)descriptorListStreamEventDescriptor.EventId);
			NpgsqlDataReader dataReader = command.ExecuteReader();
			bool result = dataReader.Read();
			dataReader.Close();
			command.Dispose();
			return result;
		}
	}
}
