﻿using Seat_Share.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Seat_Share.View
{
    /// <summary>
    /// Search page which displays all entries into the db in a list
    /// </summary>
    public sealed partial class SeatSearch : Page
    {
        string path;
        SQLite.Net.SQLiteConnection conn;

        public SeatSearch()
        {
            this.InitializeComponent();

            //Create a connection to sqlite
            connectToSQLite();

            ObservableCollection<RouteModel> routes = getSQLiteRoutes();

            //Bind the collection to the view
            RouteListView.ItemsSource = routes;
        }

        private ObservableCollection<RouteModel> getSQLiteRoutes()
        {
            //Checking the local db for any route objects and storing then into an 
            //Observable collection named routes.
            ObservableCollection<RouteModel> routes = new ObservableCollection<RouteModel>();
            var query = conn.Table<RouteModel>();

            //Loop over data in the db and store in objects 
            foreach (var data in query)
            {
                RouteModel x = new RouteModel()
                {
                    From_route = data.From_route,
                    To_route = data.To_route,
                    Trip_price = data.Trip_price,
                    Departure_time = data.Departure_time,
                    Arival_time = data.Arival_time,
                    Seat_number = data.Seat_number,
                    Contact_no = data.Contact_no,
                    Pickup_location = data.Pickup_location
                };
                //Add objects to Observable collection.
                routes.Add(x);
            }

            //Return Collection
            return routes;
        }

        private void connectToSQLite()
        {
            //Create a connection to route table in the sqlite db 
            path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
            "db.route");
            conn = new SQLite.Net.SQLiteConnection(new
               SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
        }

        private void RouteListView_Tapped(object sender, TappedRoutedEventArgs e)
        {

        }

        private async void Accept_btn_Tapped(object sender, TappedRoutedEventArgs e)
        {
            //Would update in SQLite db then display this message
            var dialog = new MessageDialog("One Seat has been reserved!" );
            await dialog.ShowAsync();
        }

        private void Map_btn_Tapped(object sender, TappedRoutedEventArgs e)
        {

        }

        private void Back_btn_Tapped(object sender, TappedRoutedEventArgs e)
        {
            //Navigate back to the main page
            Frame.Navigate(typeof(MainPage));
        }

        private void Watch_btn_Tapped(object sender, TappedRoutedEventArgs e)
        {
            
        }

        private void Accept_btn_Click(object sender, RoutedEventArgs e)
        {
      
        }
    }
}
