using Npgsql;
using skyscraper5.Dvb.DataBroadcasting.IntModel;
using skyscraper5.Skyscraper.Scraper.Storage.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Data.PostgreSql
{
    public partial class PostgresqlDataStore
    {
        private HashSet<DatabaseKeyInt> _knownInts;

        public bool TestForIpMacNotification(IpMacNotification notification)
        {
            if (_knownInts == null)
                _knownInts = new HashSet<DatabaseKeyInt>();

            DatabaseKeyInt key = new DatabaseKeyInt(notification.Platform.PlatformId, notification.Target.GenerateId());
            if (_knownInts.Contains(key))
                return true;

            bool result;
            using (NpgsqlConnection conn = new NpgsqlConnection(connectionStringBuilder.ToString()))
            {
                conn.Open();
                NpgsqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT dateadded FROM dvb_int WHERE platform_id = @id";
                cmd.Parameters.AddWithValue("@id", NpgsqlTypes.NpgsqlDbType.Bigint, (long)notification.Platform.PlatformId);
                NpgsqlDataReader reader = cmd.ExecuteReader();
                result = reader.Read();
                reader.Close();
                cmd.Dispose();
                conn.Close();
            }
            if (result)
                _knownInts.Add(key);
            return result;
        }

        public void StoreIpMacNotification(IpMacNotification notification)
        {
            EnqueueTask(x => WriteIpMacNotification(x, notification));
            DatabaseKeyInt key = new DatabaseKeyInt(notification.Platform.PlatformId, notification.Target.GenerateId());
            _knownInts.Add(key);
        }

        private void WriteIpMacNotification(NpgsqlConnection conn, IpMacNotification notification)
        {
            NpgsqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "insert into dvb_int (platform_id, platform_name, platform_provider_name,\r\n                     platform_provider_name_language_code, platform_action_type, platform_name_language_code,\r\n                     platform_processing_order, target_type, target_name, operational_network_id,\r\n                     operational_mpe_fec_algorithm, operational_component_tag, operational_transport_stream_id,\r\n                     operational_time_slice_fec_id, operational_time_slicing, operational_onid, operational_sid,\r\n                     operational_time_slice_frame_size, operational_time_slice_max_average_rate,\r\n                     operational_time_slice_max_bust_duration) " +
                "values " +
                "(@platform_id,@platform_name,@platform_provider_name,@platform_provider_name_language_code,@platform_action_type,@platform_name_language_code,@platform_processing_order,@target_type,@target_name,@operational_network_id,@operational_mpe_fec_algorithm, @operational_component_tag, @operational_transport_stream_id,\r\n                     @operational_time_slice_fec_id, @operational_time_slicing, @operational_onid, @operational_sid,\r\n                     @operational_time_slice_frame_size, @operational_time_slice_max_average_rate,\r\n                     @operational_time_slice_max_bust_duration\r\n\r\n       )";
            cmd.Parameters.AddWithValue("@platform_id", NpgsqlTypes.NpgsqlDbType.Bigint, (long)notification.Platform.PlatformId);
            cmd.Parameters.AddWithValue("@platform_name", NpgsqlTypes.NpgsqlDbType.Text, notification.Platform.Name);
            cmd.Parameters.AddWithValue("@platform_provider_name", NpgsqlTypes.NpgsqlDbType.Text, notification.Platform.ProviderName);
            cmd.Parameters.AddWithValue("@platform_provider_name_language_code", NpgsqlTypes.NpgsqlDbType.Varchar, notification.Platform.ProviderNameLanguageCode);
            cmd.Parameters.AddWithValue("@platform_action_type", NpgsqlTypes.NpgsqlDbType.Integer, notification.Platform.ActionType);
            cmd.Parameters.AddWithValue("@platform_name_language_code", NpgsqlTypes.NpgsqlDbType.Varchar, notification.Platform.NameLanguageCode);
            cmd.Parameters.AddWithValue("@platform_processing_order", NpgsqlTypes.NpgsqlDbType.Integer, notification.Platform.ProcessingOrder);
            cmd.Parameters.AddWithValue("@target_type", NpgsqlTypes.NpgsqlDbType.Integer, notification.Target.GenerateId());
            cmd.Parameters.AddWithValue("@target_name", NpgsqlTypes.NpgsqlDbType.Text, notification.Target.GenerateName());
            //@operational_network_id,@operational_mpe_fec_algorithm,@operational_component_tag,@operational_transport_stream_id,@operational_time_slice_fec_id,@operational_time_slicing,@operational_onid,@operational_sid,@operational_time_slice_frame_size, @operational_time_slice_max_average_rate,\r\n                     @operational_time_slice_max_bust_duration\r\n\r\n       )";
            cmd.Parameters.AddWithValue("@operational_network_id", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.NetworkId);
            cmd.Parameters.AddWithValue("@operational_mpe_fec_algorithm", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.MpeFecAlgorithm);
            cmd.Parameters.AddWithValue("@operational_component_tag", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.ComponentTag);
            cmd.Parameters.AddWithValue("@operational_transport_stream_id", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.TransportStreamId);
            cmd.Parameters.AddWithValue("@operational_time_slice_fec_id", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.TimeSliceFecId);
            cmd.Parameters.AddWithValue("@operational_time_slicing", NpgsqlTypes.NpgsqlDbType.Boolean, (bool?)notification.Operational.TimeSlicing);
            cmd.Parameters.AddWithValue("@operational_onid", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.OriginalNetworkId);
            cmd.Parameters.AddWithValue("@operational_sid", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.ServiceId);
            //@operational_time_slice_frame_size,@operational_time_slice_max_average_rate,@operational_time_slice_max_bust_duration
            cmd.Parameters.AddWithValue("@operational_time_slice_frame_size", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.TimeSliceFrameSize);
            cmd.Parameters.AddWithValue("@operational_time_slice_max_average_rate", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.TimeSliceMaxAverageRate);
            cmd.Parameters.AddWithValue("@operational_time_slice_max_bust_duration", NpgsqlTypes.NpgsqlDbType.Integer, (int?)notification.Operational.TimeSliceMaxBurstDuration);
            cmd.Parameters.CheckNulls();
            cmd.ExecuteNonQuery();
        }
    }
}
