using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Microsoft.Data.SqlClient;

namespace test2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        static string connectionString = "Server=(localdb)\\MSSQLLocalDB;;AttachDbFilename=C:\\Users\\Nikopol\\Documents\\a.mdf;Integrated Security=True;";
        static SqlConnection connection = new SqlConnection(connectionString);
        static List<int> ids = new List<int>();
        List<String> names = new List<String>();
        List<String> desc = new List<String>();
        public MainWindow()
        {
            InitializeComponent();
            connection.Open();
            Fill();
        }
        void Fill()
        {
            objTable.Items.Clear();
            ids.Clear();
            names.Clear();
            desc.Clear();   
            string sqlExpression = "SELECT * FROM test";
            SqlCommand command = new SqlCommand(sqlExpression, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                object id = reader.GetValue(0);
                object Name = reader.GetValue(1);
                object Description = reader.GetValue(2);
                objTable.Items.Add(id + " - " + Name + " - " + Description);
                ids.Add(Int32.Parse(id.ToString()));
                names.Add(Name.ToString());
                desc.Add(Description.ToString());
            }
            reader.Close();
        }

        private void objTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                box1.Text = names[objTable.SelectedIndex].ToString();
                box2.Text = desc[objTable.SelectedIndex].ToString();
            } catch (Exception)
            {

            }
            
        }

        private void addbtn_Click(object sender, RoutedEventArgs e)
        {
            string sqlExpression = "INSERT INTO test (id, Name, Description)  VALUES (" +
                (ids[ids.Count - 1]+1) + ", '" +
                box1.Text + "', '" +
                box2.Text + "');";
            SqlCommand command = new SqlCommand(sqlExpression, connection);
            command.ExecuteNonQuery();
            Fill();
        }

        private void removebtn_Click(object sender, RoutedEventArgs e)
        {
            string sqlExpression = "DELETE FROM test WHERE id='" +
                (ids[ids.Count - 1]+1) + "';";
            SqlCommand command = new SqlCommand(sqlExpression, connection);
            command.ExecuteNonQuery();
            Fill();
        }

        private void editbtn_Click(object sender, RoutedEventArgs e)
        {
            string sqlExpression = "UPDATE test SET Name='" +
                box1.Text + "', Description='" +
                box2.Text + "' WHERE id='" +
                ids[objTable.SelectedIndex] + "';";
            SqlCommand command = new SqlCommand(sqlExpression, connection);
            command.ExecuteNonQuery();
            Fill();
        }
    }
}
