using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Spread11Test
{
    public static class ExtMeth
    {
        public static Decimal ToDecimal(this Object obj)
        {
            return Convert.ToDecimal(obj);
        }

        public static Int32 GbActiveColIndex(this FpSpread grid)
        {
            return grid.Sheets[0].ActiveColumnIndex;
        }

        public static Cell GbCell(this FpSpread grid, Int32 row, Int32 col)
        {
            return grid.Sheets[0].Cells[row, col];
        }

        public static Cell GbCell(this FpSpread grid, Int32 row, Enum col)
        {
            return grid.Sheets[0].Cells[row, col.ToInt()];
        }

        public static Column GbCol(this FpSpread grid, Enum col)
        {
            return grid.Sheets[0].Columns[col.ToInt()];
        }

        public static Columns GbCols(this FpSpread grid)
        {
            return grid.Sheets[0].Columns;
        }

        public static List<Int32> GbColsIndex(this FpSpread grid)
        {
            return grid.Sheets[0].Columns.OfType<Column>().Select(col => col.Index).ToList(); ;
        }

        public static Row GbRow(this FpSpread grid, Int32 row)
        {
            return grid.Sheets[0].Rows[row];
        }

        public static Int32 GbRowCount(this FpSpread grid)
        {
            return grid.Sheets[0].RowCount;
        }

        public static Rows GbRows(this FpSpread grid)
        {
            return grid.Sheets[0].Rows;
        }

        public static void GbSelection(this FpSpread grid, Int32 row, Int32 col, Int32 rowCount, Int32 colCount)
        {
            var ds = new DefaultSheetSelectionModel
            {
                SelectionPolicy = SelectionPolicy.MultiRange,
                SelectionUnit = SelectionUnit.Cell
            };

            grid.Focus(); //se si chiama il metodo da un controllo diverso dalla spread, non si vede la selezione altrimenti
            grid.Sheets[0].Models.Selection = ds;
            ds.SetSelection(row, col, rowCount, colCount);
            ds.AddSelection(row, col, rowCount, colCount);
        }

        public static void GbSetActiveCell(this FpSpread grid, Int32 row, Int32 col)
        {
            grid.Sheets[0].OperationMode = OperationMode.RowMode;
            grid.Sheets[0].SetActiveCell(row, col);
        }

        public static Boolean IsNullDbNullOrEmpty(this Object obj)
        {
            return obj == null || obj == DBNull.Value || String.IsNullOrEmpty(obj.ToString());
        }

        public static Boolean IsNumeric(this String value)
        {
            Double n;
            return Double.TryParse(value, out n);
        }

        public static Boolean IsStringNotNullOrEmpty(this String input)
        {
            return !String.IsNullOrEmpty(input);
        }

        public static Boolean IsStringNullOrEmpty(this String input)
        {
            return String.IsNullOrEmpty(input);
        }

        public static Char ToChar(this Object obj)
        {
            return Convert.ToChar(obj);
        }

        public static Double ToDoubleRound2(this Object obj)
        {
            return Math.Round(Convert.ToDouble(obj), 2, MidpointRounding.AwayFromZero);
        }

        public static Int32 ToInt(this Object obj)
        {
            return Convert.ToInt32(obj);
        }
    }
}
