using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using WpfLocalization;
using System.Threading;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Collections;
using System.IO.Ports;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Configuration;
using System.Collections.Specialized;
using System.Data.SqlServerCe;
using System.ComponentModel;
using System.Windows.Threading;

namespace WpfApplication19
{
	/// <summary>
	/// Interaction logic for EditUsingTime.xaml
	/// </summary>
	public partial class EditUsingTime : Window
	{
		SqlCeConnection _opConn;
		public EditUsingTime()
		{
			InitializeComponent();
			initDatabase();
			initUI();
		}
		void initDatabase()
		{
			try
			{
				_opConn = Utils.createSqlCeConnection("operatingDbCS");


			}
			catch (SqlCeException se)
			{
				MessageBox.Show("operatingDbCS : ", se.Message);
			}
		}
		void initUI()
		{
			obInput.Text = getUsingTime("OB").ToString();
			ocInput.Text = getUsingTime("OC").ToString();
			rhInput.Text = getUsingTime("RH").ToString();
		}
		private int getUsingTime(string header)
		{
			var reader = Utils.Query(_opConn, "select * from UsingTime");
			reader.Read();
			int v = 0;
			switch (header)
			{
				case "OB":
					v = Convert.ToInt32(reader["ob"]);

					break;
				case "OC":
					v = Convert.ToInt32(reader["oc"]);
					break;
				case "RH":
					v = Convert.ToInt32(reader["rh"]);
					break;
				default:
					MessageBox.Show("Bad Header Name");
					break;
			}
			reader.Close();


			return v;
		}

		private void saveBtn_Click(object sender, RoutedEventArgs e)
		{
			updateUsingTime("OB", Convert.ToInt32(obInput.Text));
			updateUsingTime("OC", Convert.ToInt32(ocInput.Text));
			updateUsingTime("RH", Convert.ToInt32(rhInput.Text));
			//Application.Current.MainWindow.Show();
			var w = (MainWindow)Application.Current.MainWindow;
			w.refreshUsingTime();
			Close();
		}
		private void updateUsingTime(string p, int n = -1)
		{

			switch (p)
			{
				case "OB":
					Utils.Query(_opConn, "update UsingTime set ob = " + n);
					break;
				case "OC":


					Utils.Query(_opConn, "update UsingTime set oc = " + n);
					break;
				case "RH":

					Utils.Query(_opConn, "update UsingTime set rh = " + n);

					break;
				default:
					MessageBox.Show("Header Name Error");
					break;
			}
		}
	}
}
