using System;
using System.IO;
using System.Windows;
using GrapeCity.Wpf.SpreadSheet;
using GrapeCity.Spreadsheet.IO;
using GrapeCity.Spreadsheet;

namespace XLSX_Viewer_WPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadEmbeddedExcelFile();
        }

        private void LoadEmbeddedExcelFile()
        {
            try
            {
                // Step 1: Build the full path to the Excel file in the project folder
                string relativePath = "EventBudget.xlsx";
                string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);

                // Step 2: Load the file into the GcSpreadSheet control
                spreadSheet1.OpenExcel(fullPath);
                // Step 3: Access the first (or active) worksheet
                GrapeCity.Spreadsheet.IWorkbook workbook = spreadSheet1.Workbook;
                var sheet = workbook.ActiveSheet;

                // Step 4: Unlock the range you want users to be able to edit
                sheet.Cells["B4:B10"].Locked = false;

                // Step 5: Protect the worksheet (this makes Locked cells read-only in the UI)
                sheet.Protect();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Failed to load or protect Excel file:\n{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
