/*
 * Copyright 2018-2019 Chris Cromer
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Sernatur {
	using Constants;
	using LibSernatur.Misc;
	using LibSernatur.DB;

	/**
	 * The query window class
	 */
	[GtkTemplate (ui = "/cl/cromer/ubb/sernatur/query.window.ui")]
	public class QueryWindow : Gtk.ApplicationWindow {
		/**
		 * The open database connection
		 */
		private Connection conn;
		/**
		 * The columns of the tree view
		 */
		private enum Column {
			/**
			 * The tour name
			 */
			REGION_NAME,
			/**
			 * The individual cost
			 */
			QUANTITY,
			/**
			 * The number of colums in this enum
			 */
			N_COLUMNS
		}
		/**
		 * The list that stores the contents in the tree view
		 */
		private Gtk.ListStore list_store;
		/**
		 * The scroll window where the columns and rows are stored
		 */
		[GtkChild]
		private Gtk.ScrolledWindow scroll_window;
		[GtkChild]
		private Gtk.Label statement;
		[GtkChild]
		private Gtk.Label sql;
		[GtkChild]
		private Gtk.Button run_query;
		[GtkChild]
		private Gtk.Button close_query;
		private Gtk.TreeView query_tree;

		/**
		 * This callback is called when a button is clicked
		 * @param button The button that was clicked
		 */
		[GtkCallback]
		public void on_clicked_button (Gtk.Button button) {
			if (button == run_query) {
				list_store.clear ();
				var region_list = RegionesSinDescuento.get_regions (conn.db);

				region_list.foreach ((entry) => {
					Gtk.TreeIter iter;
					list_store.append (out iter);
					list_store.set (iter,
						Column.REGION_NAME, entry.nombre_region,
						Column.QUANTITY, entry.cantidad);
				});
			}
			else if (button == close_query) {
				this.close ();
			}
		}

		/**
		 * Initialize the tour list class
		 * @param application The application used to make the GLib object
		 * @param conn The database connection to use
		 */
		public QueryWindow (Gtk.Application application, Connection conn) {
			GLib.Object (application: application);
			this.conn = conn;

			// Load scroll window's content
			var builder = new Gtk.Builder ();
			try {
				builder.add_from_resource ("/cl/cromer/ubb/sernatur/query.1.ui");
				builder.connect_signals (null);
				query_tree = builder.get_object ("query_tree") as Gtk.TreeView;
				query_tree.set_visible (true);

				// Add logo to scroll window
				scroll_window.add (query_tree);
			}
			catch (Error e) {
				// This error is not fatal, so let's keep going
				warning (e.message);
			}

			this.set_visible (true); // This fixes: Gtk-CRITICAL **: 23:58:22.139: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GtkScrollbar
		}

		/**
		 * Initialize what is needed for this window
		 */
		public void initialize () {
			this.set_title ("Test");

			list_store = new Gtk.ListStore (Column.N_COLUMNS,
				typeof (string),
				typeof (uint));

			query_tree.set_model (list_store);

			statement.set_text (Q1_STATEMENT);

			sql.set_text (Q1_SQL);
		}
	}
}
