using MySqlConnector;
using skyscraper5.Teletext;
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
	{


		public bool TestForTeletextPage(int networkId, int transportStreamId, ushort programNumber, TeletextMagazine magazine,
			DateTime timestamp)
		{
			ushort pageNo = magazine.HumanReadablePageNumber;
			if (pageNo > 999)
				return false;

			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				MySqlCommand command = connection.CreateCommand();
				command.CommandText =
					"SELECT dateadded FROM teletext_page_cache WHERE nid = @nid AND tsid = @tsid AND progno = @progno AND magazine =  @magazine AND timestamp = @timestamp";
				command.Parameters.AddWithValue("@nid", networkId);
				command.Parameters.AddWithValue("@tsid", transportStreamId);
				command.Parameters.AddWithValue("@progno", programNumber);
				command.Parameters.AddWithValue("@magazine", magazine.HumanReadablePageNumber);
				command.Parameters.AddWithValue("@timestamp", timestamp);
				MySqlDataReader dataReader = command.ExecuteReader();
				bool result = dataReader.Read();
				dataReader.Close();
				return result;
			}
		}

		public void MarkTeletextPageAsKnown(int networkId, int transportStreamId, ushort programNumber, TeletextMagazine magazine,
			DateTime timestamp)
		{
			using (MySqlConnection connection = new MySqlConnection(_mcsb.ToString()))
			{
				connection.Open();
				MySqlCommand command = connection.CreateCommand();
				command.CommandText =
					"INSERT INTO teletext_page_cache (nid, tsid, progno, magazine, timestamp) VALUES (@nid, @tsid, @progno, @magazine, @timestamp)";
				command.Parameters.AddWithValue("@nid", networkId);
				command.Parameters.AddWithValue("@tsid", transportStreamId);
				command.Parameters.AddWithValue("@progno", programNumber);
				command.Parameters.AddWithValue("@magazine", magazine.HumanReadablePageNumber);
				command.Parameters.AddWithValue("@timestamp", timestamp);
				command.ExecuteNonQuery();
			}
		}

	}
}
