/*
 * 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 LibSernatur.Person;
	using LibSernatur.DB;

	/**
	 * The TourWindow class
	 */
	[GtkTemplate (ui = "/cl/cromer/ubb/sernatur/tour.window.ui")]
	public class TourWindow : Gtk.ApplicationWindow {
		/**
		 * The open database connection
		 */
		private unowned Postgres.Database conn;
		/**
		 * The columns of the tree view
		 */
		private enum Columns {
			/**
			 * The toggle
			 */
			TOGGLE,
			/**
			 * The tour name
			 */
			TOUR_NAME,
			/**
			 * The individual cost
			 */
			INDIV_COST,
			/**
			 * The group cost
			 */
			GROUP_COST,
			/**
			 * The minimum people
			 */
			MINIMUM_PEOPLE,
			/**
			 * The name of the city
			 */
			CITY,
			/**
			 * The name of the region
			 */
			REGION,
			/**
			 * 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 tree view widget
		 */
		[GtkChild]
		private Gtk.TreeView tour_tree;

		/**
		 * This callback is called when the user clicks a radio button next to a row
		 * @param toggle The toggle clicked
		 * @param path The row clicked
		 */
		[GtkCallback]
		private void toggled(Gtk.CellRendererToggle toggle, string path) {
			Gtk.TreeModelForeachFunc deselect_all = (model, path, iter) => {
				list_store.set (iter, Columns.TOGGLE, false);
				return false;
			};

			// Backup the previous state
			var tree_path = new Gtk.TreePath.from_string (path);
			Gtk.TreeIter iter;
			bool old_val;
			list_store.get_iter (out iter, tree_path);
			list_store.get (iter, Columns.TOGGLE, out old_val);

			// Deselect all states
			list_store.foreach (deselect_all);

			// Invert previous state
			list_store.get_iter (out iter, tree_path);
			list_store.set (iter, Columns.TOGGLE, !old_val);
		}

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

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

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

			Gtk.TreeIter iter;
			var tour = Tour.get_all_tours (conn);
			for (int i = 0; i < tour.length - 1; i++) {
				list_store.append (out iter);
				list_store.set (iter,
					Columns.TOGGLE, false,
					Columns.TOUR_NAME, tour[i].nombre_tour,
					Columns.INDIV_COST, tour[i].costo_indiv.to_string (),
					Columns.GROUP_COST, tour[i].costo_grupal.to_string (),
					Columns.MINIMUM_PEOPLE, tour[i].minima_personas,
					Columns.CITY, tour[i].ciudad.nombre_ciudad,
					Columns.REGION, tour[i].ciudad.region.nombre_region);
			}

			tour_tree.set_model (list_store);
		}
	}
}
