using System;

namespace QC.CorpTest.Libraries.ExcelLibrary;

public class ExcelCell
{
	private int rowIndex;

	private int columnIndex;

	private string workBookName = "";

	private string workSheetName = "";

	public string WorksheetName
	{
		get
		{
			return workSheetName;
		}
		set
		{
			workSheetName = value;
		}
	}

	public string WorkbookName
	{
		get
		{
			return workBookName;
		}
		set
		{
			workBookName = value;
		}
	}

	public int RowIndex
	{
		get
		{
			return rowIndex;
		}
		set
		{
			rowIndex = value;
		}
	}

	public int ColumnIndex
	{
		get
		{
			return columnIndex;
		}
		set
		{
			columnIndex = value;
		}
	}

	public bool IsValid()
	{
		if (rowIndex > 0 && columnIndex > 0)
		{
			return true;
		}
		return false;
	}

	public ExcelCell(int rowIndex, int columnIndex)
	{
		this.rowIndex = rowIndex;
		this.columnIndex = columnIndex;
	}

	public ExcelCell(int rowIndex, string columnString)
	{
		this.rowIndex = rowIndex;
		columnIndex = GetColumnIndex(columnString);
	}

	public ExcelCell(string cellAddress)
	{
		ParseCellAddress(cellAddress, out rowIndex, out columnIndex);
	}

	public string GetCellAddressInR1C1()
	{
		return "R" + rowIndex + "C" + columnIndex;
	}

	public string GetRelativeCellAdressInR1C1(ExcelCell anchorCell)
	{
		int num = anchorCell.RowIndex - rowIndex;
		int num2 = anchorCell.ColumnIndex - columnIndex;
		return "R[" + num + "]C[" + num2 + "]";
	}

	public string GetCellAddressInA1()
	{
		string text = "";
		try
		{
			int a = ColumnIndex - 1;
			int result = -1;
			int num = Math.DivRem(a, 26, out result);
			if (num > 0)
			{
				int value = num + 64;
				text = Convert.ToChar(value).ToString();
			}
			if (result > -1)
			{
				int value2 = result + 65;
				text += Convert.ToChar(value2);
			}
			return text + rowIndex;
		}
		catch (Exception innerException)
		{
			throw new ExcelException("Error calculating cell address in A1 format", innerException);
		}
	}

	private int GetColumnIndex(string column)
	{
		try
		{
			column = column.ToUpper();
			if (column.Length <= 2 && column.Length > 0)
			{
				int num = 0;
				char[] array = column.ToCharArray();
				if (array.Length > 0)
				{
					int num2 = Convert.ToInt32(array[0]);
					num += (num2 - 64) * 26;
				}
				if (array.Length > 1)
				{
					int num3 = Convert.ToInt32(array[1]);
					num += num3 - 64;
				}
				return num;
			}
			throw new ArgumentException("Column string not in the right format");
		}
		catch (Exception innerException)
		{
			throw new ExcelException("Error calculating column index from column string", innerException);
		}
	}

	private void ParseCellAddress(string cellAddress, out int rowIndex, out int columnIndex)
	{
		rowIndex = -1;
		columnIndex = -1;
		if (cellAddress.Contains("R") && cellAddress.Contains("C") && !cellAddress.Contains("RC"))
		{
			string[] array = cellAddress.Split(new string[2] { "R", "C" }, StringSplitOptions.RemoveEmptyEntries);
			if (array.Length != 2)
			{
				throw new ArgumentException("Input cell address not in the right format - " + cellAddress);
			}
			rowIndex = Convert.ToInt32(array[0]);
			columnIndex = Convert.ToInt32(array[1]);
		}
	}

	public ExcelCell Copy()
	{
		return new ExcelCell(rowIndex, columnIndex);
	}

	public bool IsSame(ExcelCell cell)
	{
		if (cell.rowIndex == rowIndex && cell.columnIndex == columnIndex)
		{
			return true;
		}
		return false;
	}
}
