using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iDiTect.Excel.Demo
{
    public static class ProtectAndLockHelper
    {
        public static void ProtectWorksheet()
        {
            var workbook = new Workbook();
            var worksheet = workbook.Worksheets.Add("Protect with no password");

            //Only allow to edit style in this worksheet
            worksheet.Protect().AllowElement
            (                
                SheetProtectionElements.FormatCells
                | SheetProtectionElements.FormatRows
                | SheetProtectionElements.FormatColumns
            );

            worksheet.Cell(1, 1).Value = "Lock Cell";
            worksheet.Cell(1, 1).Style.Font.FontColor = ExcelColor.Red;
            //If the worksheet is protect(), all the cell is lock by default
            //worksheet.Cell("A1").Style.Protection.Locked = true;

            //Get column B
            var column = worksheet.Column(2);
            column.Style.Fill.BackgroundColor = ExcelColor.Blue;
            //Unlock column B
            column.Style.Protection.Locked = false;

            //Get the sixth row
            var row = worksheet.Row(6);
            worksheet.Cell(6, 1).Value = "this value will be hiden";
            row.Style.Fill.BackgroundColor = ExcelColor.YellowGreen;
            //Hide the sixth row
            row.Style.Protection.Hidden = true;


            //Create another worksheet with password protected
            var worksheet2 = workbook.Worksheets.Add("Protect with password");
            //Allow everything to edit/insert/delete
            worksheet2.Protect("password").AllowEverything();

            workbook.Save("ProtectWorksheet.xlsx");

        }

        public static void ProtectWorkbook()
        {
            var workbook = new Workbook();
            workbook.Worksheets.Add("Protect");

            workbook.Protect("password");

            workbook.Save("ProtectWorkbook.xlsx");

        }
    }
}
