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

namespace skyscraper5.Data.MySql
{
    public partial class MySqlDataStorage : DataStorage
    {
        private void InsertScte35SpliceComponents(MySqlTransaction transaction, int currentNetworkId, int currentTransportStreamId, ushort programNumber, SpliceInsert spliceInsert)
        {
            if (spliceInsert.Components == null)
                return;

            if (spliceInsert.Components.Length == 0)
                return;

            MySqlCommand command = transaction.Connection.CreateCommand();
            command.Transaction = transaction;
            command.CommandText =
                "INSERT INTO scte35_splice_components (cnid, ctsid, progno, splice_event_id, k, v) VALUES (@cnid, @ctsid, @progno, @splice_event_id, @k, @v)";
            command.Parameters.AddWithValue("@cnid", currentNetworkId);
            command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
            command.Parameters.AddWithValue("@progno", programNumber);
            command.Parameters.AddWithValue("@splice_event_id", spliceInsert.SpliceEventId);
            command.Parameters.Add("@k", MySqlDbType.Int16);
            command.Parameters.Add("@v", MySqlDbType.Int64);
            foreach (Tuple<byte, ulong?> component in spliceInsert.Components)
            {
                command.Parameters["@k"].Value = component.Item1;
                command.Parameters["@v"].Value = component.Item2;
                command.ExecuteNonQuery();
            }
        }

        private void InsertScte35SpliceSegmentationComponents(MySqlTransaction transaction, int currentNetworkId, int currentTransportStreamId, ushort programNumber, SpliceInsert spliceInsert)
        {
            if (spliceInsert.Descriptors == null)
                return;

            if (spliceInsert.Descriptors.segmentationDescriptor == null)
                return;

            SegmentationDescriptor.Component[] components = spliceInsert.Descriptors.segmentationDescriptor.Components;
            if (components == null)
                return;

            if (components.Length == 0)
                return;

            MySqlCommand command = transaction.Connection.CreateCommand();
            command.Transaction = transaction;
            command.CommandText =
                "INSERT INTO scte35_splice_segmentation_components (cnid, ctsid, progno, splice_event_id, component_tag, pts_offset) " +
                "VALUES (@cnid, @ctsid, @progno, @splice_event_id, @component_tag, @pts_offset)";
            command.Parameters.AddWithValue("@cnid", currentNetworkId);
            command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
            command.Parameters.AddWithValue("@progno", programNumber);
            command.Parameters.AddWithValue("@splice_event_id", spliceInsert.SpliceEventId);
            command.Parameters.Add("@component_tag", MySqlDbType.Int16);
            command.Parameters.Add("@pts_offset", MySqlDbType.Int64);
            foreach (SegmentationDescriptor.Component component in components)
            {
                command.Parameters["@component_tag"].Value = component.ComponentTag;
                command.Parameters["@pts_offset"].Value = component.PtsOffset;
                command.ExecuteNonQuery();
            }
        }

        private DateTime? QueryScte35TimeSignal(MySqlConnection connection, int currentNetworkId, int currentTransportStreamId, ushort programNumber)
        {
            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "SELECT dateadded FROM scte35_time_signal WHERE cnid = @cnid AND ctsid = @ctsid AND progno = @progno";
            command.Parameters.AddWithValue("@cnid", currentNetworkId);
            command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
            command.Parameters.AddWithValue("@progno", programNumber);
            MySqlDataReader dataReader = command.ExecuteReader();
            DateTime? result = null;
            if (dataReader.Read())
                result = dataReader.GetDateTime(0);
            dataReader.Close();
            return result;
        }

		public bool TestForScte35SpliceInsert(int currentNetworkId, int currentTransportStreamId, ushort programNumber,
	SpliceInsert spliceInsert)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				MySqlCommand command = connection.CreateCommand();
				command.CommandText =
					"SELECT dateadded FROM scte35_splice WHERE cnid = @cnid AND ctsid = @ctsid AND progno = @progno AND splice_event_id = @splice_event_id";
				command.Parameters.AddWithValue("@cnid", currentNetworkId);
				command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
				command.Parameters.AddWithValue("@progno", programNumber);
				command.Parameters.AddWithValue("@splice_event_id", spliceInsert.SpliceEventId);
				MySqlDataReader dataReader = command.ExecuteReader();
				bool result = dataReader.Read();
				dataReader.Close();
				connection.Close();
				return result;
			}
		}

		public void StoreScte35SpliceInsert(int currentNetworkId, int currentTransportStreamId, ushort programNumber,
			SpliceInsert spliceInsert)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				MySqlTransaction transaction = connection.BeginTransaction();
				MySqlCommand command = connection.CreateCommand();
				command.Transaction = transaction;
				command.CommandText =
					"INSERT INTO scte35_splice (cnid, ctsid, progno, splice_event_id, splice_time, splice_event_cancel_indicator, avails_expected, avail_num, unique_program_id, duration, duration_auto_return, splice_immediate_flag, duration_flag, program_splice_flag, out_of_network_indicator, provider_avail_id, identifier, sub_segments_expected, sub_segment_num, segments_expected, segment_num, segmentation_type_id, segmentation_upid, segmentation_upid_type, segmentation_duration, program_segmentation_flag, segmentation_event_cancel_indicator, device_restrictions, archive_allowed_flag, no_regional_blackout_flag, web_delivery_flag, segmentation_duration_flag, segmentation_event_id, segmentation_identifier, utc_offset, tai_ns, tai_seconds, tai_identififer)  " +
					"VALUES  (@cnid, @ctsid, @progno, @splice_event_id, @splice_time, @splice_event_cancel_indicator, @avails_expected, @avail_num, @unique_program_id, @duration, @duration_auto_return, @splice_immediate_flag, " +
					"@duration_flag, @program_splice_flag, @out_of_network_indicator, @provider_avail_id, @identifier, @sub_segments_expected, @sub_segment_num, @segments_expected, @segment_num," +
					"@segmentation_type_id, @segmentation_upid, @segmentation_upid_type, @segmentation_duration, @program_segmentation_flag, @segmentation_event_cancel_indicator, @device_restrictions, " +
					"@archive_allowed_flag, @no_regional_blackout_flag, @web_delivery_flag, @segmentation_duration_flag, @segmentation_event_id, @segmentation_identifier, @utc_offset, @tai_ns, @tai_seconds, @tai_identififer)";
				command.Parameters.AddWithValue("@cnid", currentNetworkId);
				command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
				command.Parameters.AddWithValue("@progno", programNumber);
				command.Parameters.AddWithValue("@splice_event_id", spliceInsert.SpliceEventId);
				command.Parameters.AddWithValue("@splice_time", spliceInsert.SpliceTime);
				command.Parameters.AddWithValue("@splice_event_cancel_indicator", spliceInsert.SpliceEventCancelIndicator);
				command.Parameters.AddWithValue("@avails_expected", spliceInsert.AvailsExpected);
				command.Parameters.AddWithValue("@avail_num", spliceInsert.AvailNum);
				command.Parameters.AddWithValue("@unique_program_id", spliceInsert.UniqueProgramId);
				command.Parameters.AddWithValue("@duration", spliceInsert.Duration);
				command.Parameters.AddWithValue("@duration_auto_return", spliceInsert.DurationAutoReturn);
				command.Parameters.AddWithValue("@splice_immediate_flag", spliceInsert.SpliceImmediateFlag);
				command.Parameters.AddWithValue("@duration_flag", spliceInsert.DurationFlag);
				command.Parameters.AddWithValue("@program_splice_flag", spliceInsert.ProgramSpliceFlag);
				command.Parameters.AddWithValue("@out_of_network_indicator", spliceInsert.OutOfNetworkIndicator);
				if (spliceInsert.Descriptors != null)
				{
					if (spliceInsert.Descriptors.availDescriptor != null)
					{
						command.Parameters.AddWithValue("@provider_avail_id", spliceInsert.Descriptors.availDescriptor.ProviderAvailId);
						command.Parameters.AddWithValue("@identifier", spliceInsert.Descriptors.availDescriptor.Identifier);
					}
					else
					{
						command.Parameters.AddWithValue("@provider_avail_id", DBNull.Value);
						command.Parameters.AddWithValue("@identifier", DBNull.Value);
					}

					if (spliceInsert.Descriptors.segmentationDescriptor != null)
					{
						command.Parameters.AddWithValue("@sub_segments_expected", spliceInsert.Descriptors.segmentationDescriptor.SubSegmentsExpected);
						command.Parameters.AddWithValue("@sub_segment_num", spliceInsert.Descriptors.segmentationDescriptor.SubSegmentNum);
						command.Parameters.AddWithValue("@segments_expected", spliceInsert.Descriptors.segmentationDescriptor.SegmentsExpected);
						command.Parameters.AddWithValue("@segment_num", spliceInsert.Descriptors.segmentationDescriptor.SegmentNum);
						command.Parameters.AddWithValue("@segmentation_type_id", spliceInsert.Descriptors.segmentationDescriptor.SegmentationTypeId);
						command.Parameters.AddWithValue("@segmentation_upid", spliceInsert.Descriptors.segmentationDescriptor.SegmentationUpid);
						command.Parameters.AddWithValue("@segmentation_upid_type", spliceInsert.Descriptors.segmentationDescriptor.SegmentationUpidType);
						command.Parameters.AddWithValue("@segmentation_duration", spliceInsert.Descriptors.segmentationDescriptor.SegmentationDuration);
						command.Parameters.AddWithValue("@program_segmentation_flag", spliceInsert.Descriptors.segmentationDescriptor.ProgramSegmentationFlag);
						command.Parameters.AddWithValue("@segmentation_event_cancel_indicator", spliceInsert.Descriptors.segmentationDescriptor.SegmentationEventCancelIndicator);
						command.Parameters.AddWithValue("@device_restrictions", spliceInsert.Descriptors.segmentationDescriptor.DeviceRestrictions);
						command.Parameters.AddWithValue("@archive_allowed_flag", spliceInsert.Descriptors.segmentationDescriptor.ArchiveAllowedFlag);
						command.Parameters.AddWithValue("@no_regional_blackout_flag", spliceInsert.Descriptors.segmentationDescriptor.NoRegionalBlackoutFlag);
						command.Parameters.AddWithValue("@web_delivery_flag", spliceInsert.Descriptors.segmentationDescriptor.WebDeliveryFlag);
						command.Parameters.AddWithValue("@segmentation_duration_flag", spliceInsert.Descriptors.segmentationDescriptor.SegmentationDurationFlag);
						command.Parameters.AddWithValue("@segmentation_event_id", spliceInsert.Descriptors.segmentationDescriptor.SegmentationEventId);
						command.Parameters.AddWithValue("@segmentation_identifier", spliceInsert.Descriptors.segmentationDescriptor.Identifier);
					}
					else
					{
						command.Parameters.AddWithValue("@sub_segments_expected", DBNull.Value);
						command.Parameters.AddWithValue("@sub_segment_num", DBNull.Value);
						command.Parameters.AddWithValue("@segments_expected", DBNull.Value);
						command.Parameters.AddWithValue("@segment_num", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_type_id", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_upid", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_upid_type", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_duration", DBNull.Value);
						command.Parameters.AddWithValue("@program_segmentation_flag", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_event_cancel_indicator", DBNull.Value);
						command.Parameters.AddWithValue("@device_restrictions", DBNull.Value);
						command.Parameters.AddWithValue("@archive_allowed_flag", DBNull.Value);
						command.Parameters.AddWithValue("@no_regional_blackout_flag", DBNull.Value);
						command.Parameters.AddWithValue("@web_delivery_flag", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_duration_flag", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_event_id", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_identifier", DBNull.Value);
					}

					if (spliceInsert.Descriptors.timeDescriptor != null)
					{
						command.Parameters.AddWithValue("@segmentation_identifier", spliceInsert.Descriptors.timeDescriptor.UtcOffset);
						command.Parameters.AddWithValue("@tai_ns", spliceInsert.Descriptors.timeDescriptor.TaiNs);
						command.Parameters.AddWithValue("@tai_seconds", spliceInsert.Descriptors.timeDescriptor.TaiSeconds);
						command.Parameters.AddWithValue("@tai_identififer", spliceInsert.Descriptors.timeDescriptor.Identifier);
					}
					else
					{
						command.Parameters.AddWithValue("@utc_offset", DBNull.Value);
						command.Parameters.AddWithValue("@tai_ns", DBNull.Value);
						command.Parameters.AddWithValue("@tai_seconds", DBNull.Value);
						command.Parameters.AddWithValue("@tai_identififer", DBNull.Value);
					}
				}
				else
				{
					command.Parameters.AddWithValue("@provider_avail_id", DBNull.Value);
					command.Parameters.AddWithValue("@identifier", DBNull.Value);
					command.Parameters.AddWithValue("@sub_segments_expected", DBNull.Value);
					command.Parameters.AddWithValue("@sub_segment_num", DBNull.Value);
					command.Parameters.AddWithValue("@segments_expected", DBNull.Value);
					command.Parameters.AddWithValue("@segment_num", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_type_id", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_upid", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_upid_type", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_duration", DBNull.Value);
					command.Parameters.AddWithValue("@program_segmentation_flag", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_event_cancel_indicator", DBNull.Value);
					command.Parameters.AddWithValue("@device_restrictions", DBNull.Value);
					command.Parameters.AddWithValue("@archive_allowed_flag", DBNull.Value);
					command.Parameters.AddWithValue("@no_regional_blackout_flag", DBNull.Value);
					command.Parameters.AddWithValue("@web_delivery_flag", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_duration_flag", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_event_id", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_identifier", DBNull.Value);
					command.Parameters.AddWithValue("@utc_offset", DBNull.Value);
					command.Parameters.AddWithValue("@tai_ns", DBNull.Value);
					command.Parameters.AddWithValue("@tai_seconds", DBNull.Value);
					command.Parameters.AddWithValue("@tai_identififer", DBNull.Value);
				}

				command.ExecuteNonQuery();

				InsertScte35SpliceComponents(transaction, currentNetworkId, currentTransportStreamId, programNumber, spliceInsert);
				InsertScte35SpliceSegmentationComponents(transaction, currentNetworkId, currentTransportStreamId, programNumber, spliceInsert);
				transaction.Commit();
				connection.Close();
			}
		}

		public void SetScte35TimeSignal(int currentNetworkId, int currentTransportStreamId, DateTime currentTime, ushort programNumber, TimeSignal spliceInsert)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				MySqlCommand command = connection.CreateCommand();
				command.CommandText =
					"INSERT INTO scte35_time_signal (cnid, ctsid, progno, current, value, provider_avail_id, identifier, sub_segments_expected, sub_segment_num, segments_expected, segment_num, segmentation_type_id, segmentation_upid, segmentation_upid_type, segmentation_duration, program_segmentation_flag, segmentation_event_cancel_indicator, device_restrictions, archive_allowed_flag, no_regional_blackout_flag, web_delivery_flag, segmentation_duration_flag, segmentation_event_id, segmentation_identifier, utc_offset, tai_ns, tai_seconds, tai_identififer) " +
					"VALUES (@cnid, @ctsid, @progno, @current, @value, @provider_avail_id, @identifier, @sub_segments_expected, @sub_segment_num, @segments_expected, @segment_num, @segmentation_type_id, @segmentation_upid, @segmentation_upid_type, @segmentation_duration, @program_segmentation_flag, @segmentation_event_cancel_indicator, @device_restrictions, @archive_allowed_flag, @no_regional_blackout_flag, @web_delivery_flag, @segmentation_duration_flag, @segmentation_event_id, @segmentation_identifier, @utc_offset, @tai_ns, @tai_seconds, @tai_identififer)";
				command.Parameters.AddWithValue("@cnid", currentNetworkId);
				command.Parameters.AddWithValue("@ctsid", currentTransportStreamId);
				command.Parameters.AddWithValue("@progno", programNumber);
				command.Parameters.AddWithValue("@current", currentTime);
				if (spliceInsert.Descriptors != null)
				{
					if (spliceInsert.Descriptors.availDescriptor != null)
					{
						command.Parameters.AddWithValue("@provider_avail_id", spliceInsert.Descriptors.availDescriptor.ProviderAvailId);
						command.Parameters.AddWithValue("@identifier", spliceInsert.Descriptors.availDescriptor.Identifier);
					}
					else
					{
						command.Parameters.AddWithValue("@provider_avail_id", DBNull.Value);
						command.Parameters.AddWithValue("@identifier", DBNull.Value);
					}

					if (spliceInsert.Descriptors.segmentationDescriptor != null)
					{
						command.Parameters.AddWithValue("@sub_segments_expected", spliceInsert.Descriptors.segmentationDescriptor.SubSegmentsExpected);
						command.Parameters.AddWithValue("@sub_segment_num", spliceInsert.Descriptors.segmentationDescriptor.SubSegmentNum);
						command.Parameters.AddWithValue("@segments_expected", spliceInsert.Descriptors.segmentationDescriptor.SegmentsExpected);
						command.Parameters.AddWithValue("@segment_num", spliceInsert.Descriptors.segmentationDescriptor.SegmentNum);
						command.Parameters.AddWithValue("@segmentation_type_id", spliceInsert.Descriptors.segmentationDescriptor.SegmentationTypeId);
						command.Parameters.AddWithValue("@segmentation_upid", spliceInsert.Descriptors.segmentationDescriptor.SegmentationUpid);
						command.Parameters.AddWithValue("@segmentation_upid_type", spliceInsert.Descriptors.segmentationDescriptor.SegmentationUpidType);
						command.Parameters.AddWithValue("@segmentation_duration", spliceInsert.Descriptors.segmentationDescriptor.SegmentationDuration);
						command.Parameters.AddWithValue("@program_segmentation_flag", spliceInsert.Descriptors.segmentationDescriptor.ProgramSegmentationFlag);
						command.Parameters.AddWithValue("@segmentation_event_cancel_indicator", spliceInsert.Descriptors.segmentationDescriptor.SegmentationEventCancelIndicator);
						command.Parameters.AddWithValue("@device_restrictions", spliceInsert.Descriptors.segmentationDescriptor.DeviceRestrictions);
						command.Parameters.AddWithValue("@archive_allowed_flag", spliceInsert.Descriptors.segmentationDescriptor.ArchiveAllowedFlag);
						command.Parameters.AddWithValue("@no_regional_blackout_flag", spliceInsert.Descriptors.segmentationDescriptor.NoRegionalBlackoutFlag);
						command.Parameters.AddWithValue("@web_delivery_flag", spliceInsert.Descriptors.segmentationDescriptor.WebDeliveryFlag);
						command.Parameters.AddWithValue("@segmentation_duration_flag", spliceInsert.Descriptors.segmentationDescriptor.SegmentationDurationFlag);
						command.Parameters.AddWithValue("@segmentation_event_id", spliceInsert.Descriptors.segmentationDescriptor.SegmentationEventId);
						command.Parameters.AddWithValue("@segmentation_identifier", spliceInsert.Descriptors.segmentationDescriptor.Identifier);
					}
					else
					{
						command.Parameters.AddWithValue("@sub_segments_expected", DBNull.Value);
						command.Parameters.AddWithValue("@sub_segment_num", DBNull.Value);
						command.Parameters.AddWithValue("@segments_expected", DBNull.Value);
						command.Parameters.AddWithValue("@segment_num", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_type_id", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_upid", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_upid_type", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_duration", DBNull.Value);
						command.Parameters.AddWithValue("@program_segmentation_flag", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_event_cancel_indicator", DBNull.Value);
						command.Parameters.AddWithValue("@device_restrictions", DBNull.Value);
						command.Parameters.AddWithValue("@archive_allowed_flag", DBNull.Value);
						command.Parameters.AddWithValue("@no_regional_blackout_flag", DBNull.Value);
						command.Parameters.AddWithValue("@web_delivery_flag", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_duration_flag", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_event_id", DBNull.Value);
						command.Parameters.AddWithValue("@segmentation_identifier", DBNull.Value);
					}

					if (spliceInsert.Descriptors.timeDescriptor != null)
					{
						command.Parameters.AddWithValue("@utc_offset", spliceInsert.Descriptors.timeDescriptor.UtcOffset);
						command.Parameters.AddWithValue("@tai_ns", spliceInsert.Descriptors.timeDescriptor.TaiNs);
						command.Parameters.AddWithValue("@tai_seconds", spliceInsert.Descriptors.timeDescriptor.TaiSeconds);
						command.Parameters.AddWithValue("@tai_identififer", spliceInsert.Descriptors.timeDescriptor.Identifier);
					}
					else
					{
						command.Parameters.AddWithValue("@utc_offset", DBNull.Value);
						command.Parameters.AddWithValue("@tai_ns", DBNull.Value);
						command.Parameters.AddWithValue("@tai_seconds", DBNull.Value);
						command.Parameters.AddWithValue("@tai_identififer", DBNull.Value);
					}
				}
				else
				{
					command.Parameters.AddWithValue("@provider_avail_id", DBNull.Value);
					command.Parameters.AddWithValue("@identifier", DBNull.Value);
					command.Parameters.AddWithValue("@sub_segments_expected", DBNull.Value);
					command.Parameters.AddWithValue("@sub_segment_num", DBNull.Value);
					command.Parameters.AddWithValue("@segments_expected", DBNull.Value);
					command.Parameters.AddWithValue("@segment_num", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_type_id", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_upid", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_upid_type", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_duration", DBNull.Value);
					command.Parameters.AddWithValue("@program_segmentation_flag", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_event_cancel_indicator", DBNull.Value);
					command.Parameters.AddWithValue("@device_restrictions", DBNull.Value);
					command.Parameters.AddWithValue("@archive_allowed_flag", DBNull.Value);
					command.Parameters.AddWithValue("@no_regional_blackout_flag", DBNull.Value);
					command.Parameters.AddWithValue("@web_delivery_flag", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_duration_flag", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_event_id", DBNull.Value);
					command.Parameters.AddWithValue("@segmentation_identifier", DBNull.Value);
					command.Parameters.AddWithValue("@utc_offset", DBNull.Value);
					command.Parameters.AddWithValue("@tai_ns", DBNull.Value);
					command.Parameters.AddWithValue("@tai_seconds", DBNull.Value);
					command.Parameters.AddWithValue("@tai_identififer", DBNull.Value);
				}

				command.ExecuteNonQueryAsync().ContinueWith(
					x =>
					{
						connection.Close();
					}
					);
			}
		}

	}
}
