﻿using System;
using System.Data.SQLite;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using EcoRideDB.Res;

namespace EcoRideDB {
    public partial class MainWindow : Window {
        private readonly string connectionStr = "Data Source=EcoRideDB.db;Version=3;";

        public MainWindow() {
            InitializeComponent();
            this.Width = 1000;
            this.Height = 600;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            var monitors = MonitorHelper.GetMonitors();
            var mousePos = System.Windows.Input.Mouse.GetPosition(this);
            var screen = MonitorHelper.GetScreenForMousePos(mousePos, monitors);

            this.Left = screen.Left + (screen.Width - this.Width) / 2;
            this.Top = screen.Top + (screen.Height - this.Height) / 2;
        }

        // Navigation buttons
        private void btnHome_Click(object sender, RoutedEventArgs e) => MainContent.Content = CreateHomeView();
        private void btnAddBike_Click(object sender, RoutedEventArgs e) => MainContent.Content = new AddBike();
        private void btnAddRider_Click(object sender, RoutedEventArgs e) => MainContent.Content = new AddRider();
        private void btnAddRental_Click(object sender, RoutedEventArgs e) => MainContent.Content = new AddRental();
        private void btnAddMaintenance_Click(object sender, RoutedEventArgs e) => MainContent.Content = new AddMaintenance();
        private void btnAddStation_Click(object sender, RoutedEventArgs e) => MainContent.Content = new AddStation();

        private void btnViewBikes_Click(object sender, RoutedEventArgs e) => MainContent.Content = new BikesView();
        private void btnViewRiders_Click(object sender, RoutedEventArgs e) => MainContent.Content = new RidersView();
        private void btnViewRentals_Click(object sender, RoutedEventArgs e) => MainContent.Content = new RentalsView();
        private void btnViewMaintenance_Click(object sender, RoutedEventArgs e) => MainContent.Content = new MaintenanceView();
        private void btnViewStations_Click(object sender, RoutedEventArgs e) => MainContent.Content = new StationsView();

        private UserControl CreateHomeView() {
            var grid = new Grid();
            grid.Children.Add(new TextBlock {
                Text = "Welcome to EcoRideDB!",
                FontSize = 36,
                FontWeight = FontWeights.Bold,
                FontFamily = new System.Windows.Media.FontFamily("Segoe UI"),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Foreground = System.Windows.Media.Brushes.White
            });
            return new UserControl { Content = grid };
        }

        // Log export
        private void btnExportData_Click(object sender, RoutedEventArgs e) {
            StringBuilder logBuilder = new StringBuilder();

            logBuilder.AppendLine("----- Bikes -----");
            logBuilder.AppendLine(GetTableData("Bikes", "BikeID, Type, Condition, Status, StationID"));

            logBuilder.AppendLine("----- Riders -----");
            logBuilder.AppendLine(GetTableData("Riders", "RiderID, Name, Email, Phone, IDNumber, MembershipDate"));

            logBuilder.AppendLine("----- Rentals -----");
            logBuilder.AppendLine(GetTableData("Rentals", "RentalID, RiderID, BikeID, RentalDate, ReturnDate, Cost"));

            logBuilder.AppendLine("----- Maintenance -----");
            logBuilder.AppendLine(GetTableData("Maintenance", "MaintenanceID, BikeID, StaffID, ServiceDate, Notes"));

            logBuilder.AppendLine("----- Stations -----");
            logBuilder.AppendLine(GetTableData("Stations", "StationID, Name, Location"));

            File.WriteAllText("EcoRideDB_Logs.txt", logBuilder.ToString());
            MessageBox.Show("Logs exported to EcoRideDB_Logs.txt", "Export Complete", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private string GetTableData(string tableName, string columns) {
            StringBuilder sb = new StringBuilder();

            using (var connection = new SQLiteConnection(connectionStr)) {
                connection.Open();
                using var command = new SQLiteCommand($"SELECT {columns} FROM {tableName}", connection);
                using var reader = command.ExecuteReader();
                while (reader.Read()) {
                    for (int i = 0; i < reader.FieldCount; i++)
                        sb.Append($"{reader.GetName(i)}: {reader.GetValue(i)}, ");
                    sb.AppendLine();
                }
            }

            return sb.ToString();
        }
    }
}