using Org.BouncyCastle.Asn1.Mozilla;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InABox.Core;

namespace InABox.Scripting
{

    public enum CellType
    {
        Formula,
        Numeric,
        Error,
        String,
        Boolean,
        Blank,
        Unknown,
        Date
    }

    public class CellRange
    {
        public int FirstRow { get; set; }
        public int LastRow { get; set; }
        public int FirstColumn { get; set; }
        public int LastColumn { get; set; }

        public CellRange(int firstRow, int lastRow, int firstColumn, int lastColumn)
        {
            FirstRow = firstRow;
            LastRow = lastRow;
            FirstColumn = firstColumn;
            LastColumn = lastColumn;
        }
    }

    public interface IDataFormat {
        public short FormatIndex { get; }
    }

    public enum UnderlineType
    {
        None,
        Single,
        Double,
        SingleAccounting,
        DoubleAccounting
    }

    public interface IFont
    {
        public bool Bold { get; set; }

        public bool Italic { get; set; }

        public UnderlineType Underline { get; set; }

        public double FontSize { get; set; }

        public Color Colour { get; }
    }

    public enum CellAlignment
    {
        Start,
        Middle,
        End,
        Justify
    }

    public interface ICellStyle
    {
        public ISpreadsheet Spreadsheet { get; }

        public IDataFormat DataFormat { get; set; }

        public IFont Font { get; }

        public Color Background { get; }

        public Color Foreground { get; }
        
        public CellAlignment VerticalAlignment { get; }
        
        public CellAlignment HorizontalAlignment { get; }
        
        public bool WrapText { get; }
        
    }

    public interface ISheet
    {
        public string Name { get; }

        int FirstRow { get; }

        int LastRow { get; }

        ISpreadsheet Spreadsheet { get; }

        IRow NewRow();

        IRow? GetRow(int row);

        /// <summary>
        /// Sets the column width with a certain number of characters.
        /// </summary>
        /// <param name="column"></param>
        /// <param name="charWidth"></param>
        /// <returns></returns>
        ISheet SetColumnWidth(int column, float charWidth);

        /// <summary>
        /// Gets the width of a column in characters. Returns float.MinValue if no width has been set.
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        float GetColumnWidth(int column);

        /// <summary>
        /// Gets the height of a row in points.
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        float GetRowHeight(int row);

        float GetDefaultRowHeight();

        ISheet MergeCells(int firstRow, int lastRow, int firstColumn, int lastColumn);
        IEnumerable<CellRange> GetMergedCells();

        IEnumerable<IRow> Rows();

        IEnumerator<IRow> RowEnumerator();
    }

    public interface IRow
    {
        public int RowNumber { get; }

        int FirstColumn { get; }

        int LastColumn { get; }

        ISheet Sheet { get; }

        public ICell this[int column]
        {
            get { return GetCell(column); }
        }

        public string ExtractString(int column, bool uppercase = false);

        public DateTime ExtractDateTime(int column);

        public double? ExtractDouble(int column);

        public int GetColumn(string name, bool throwException = true);

        public ICell? GetCell(int column);

        public ICell NewCell(int column);

        public IEnumerable<ICell> Cells();
    }

    public interface ICell
    {
        IRow Row { get; }

        CellType GetCellType();

        string GetValue();
        bool? GetBoolValue();
        double? GetDoubleValue();
        DateTime GetDateTimeValue();
        byte? GetByteValue();

        ICell SetValue(bool value);
        ICell SetValue(string value);
        ICell SetValue(double value);
        ICell SetValue(DateTime value);
        ICell SetValue(byte value);

        ICell SetBlank();

        ICellStyle GetStyle();
        ICell SetStyle(ICellStyle style);
    }

    public interface ISpreadsheet
    {
        public ISheet GetSheet(int index);

        public ISheet GetSheet(string name);

        /// <summary>
        /// Make a new sheet and add it to the spreadsheet
        /// </summary>
        /// <param name="name">The name of the new sheet</param>
        /// <returns>The new sheet</returns>
        public ISheet NewSheet(string name);

        /// <summary>
        /// Make a new sheet and add it to the spreadsheet
        /// </summary>
        /// <returns>The new sheet</returns>
        public ISheet NewSheet();

        public IEnumerable<ISheet> Sheets();

        public IEnumerator<ISheet> SheetEnumerator();

        public ICellStyle NewStyle();

        public IDataFormat GetDataFormat(string format);

        public void Write(FileStream file);

        public void Write(string filename, FileMode mode = FileMode.Create);
    }
}
