using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
//using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

using OfficeOpenXml;
using System.Drawing;
using OfficeOpenXml.Style;

namespace NewsCrawler
{
	public class ExcelHandler
	{
		string m_strFileName = "";
		string m_strToday = DateTime.Now.ToString("yyyy-MM-dd");

		public ExcelHandler(string strFileName)
		{
			m_strFileName = strFileName;
		}

		private void Create()
		{
			FileInfo newFile = new FileInfo(m_strFileName);
			ExcelPackage package = new ExcelPackage(newFile);
			ExcelWorksheet Sheet = package.Workbook.Worksheets.Add("first");
			Sheet.Cells[1, 1].Value = "날짜";
			Sheet.Cells[1, 2].Value = "기사 시간";
			Sheet.Cells[1, 3].Value = "받은 시간";
			Sheet.Cells[1, 4].Value = "요청 시간";
			Sheet.Cells[1, 5].Value = "출처";
			Sheet.Cells[1, 6].Value = "제목";
			Sheet.Cells[1, 7].Value = "종목명";
			Sheet.Cells[1, 8].Value = "시가";
			Sheet.Cells[1, 9].Value = "저가";
			Sheet.Cells[1, 10].Value = "대비";
			Sheet.Cells[1, 11].Value = "고가";
			Sheet.Cells[1, 12].Value = "대비";
			Sheet.Cells[1, 13].Value = "링크";

			package.Save();
		}

		public bool AddRow(string strNewsTime, string strResTime, float fReqTime, string strRef, string strTitle, string strCodeName, 
			int iPriceS, int iPriceLow, float fPriceLowP, int iPriceHigh, float fPriceHighP, string strLink)
		{
			lock(this)
			{
				if(File.Exists(m_strFileName) == false)
					Create();

				FileInfo newFile = new FileInfo(m_strFileName);
				ExcelPackage package = new ExcelPackage(newFile);
				ExcelWorksheet Sheet = package.Workbook.Worksheets["first"];
				int iRow = Sheet.Dimension.Rows+1;
				Sheet.Cells[iRow, 1].Value = m_strToday;
				Sheet.Cells[iRow, 2].Value = strNewsTime;
				Sheet.Cells[iRow, 3].Value = strResTime;
				Sheet.Cells[iRow, 4].Value = fReqTime;
				Sheet.Cells[iRow, 5].Value = strRef;
				Sheet.Cells[iRow, 6].Value = strTitle;
				Sheet.Cells[iRow, 7].Value = strCodeName;
				Sheet.Cells[iRow, 8].Value = iPriceS;
				Sheet.Cells[iRow, 8].Style.Numberformat.Format = "###,###,##0";
				Sheet.Cells[iRow, 9].Value = iPriceLow;
				Sheet.Cells[iRow, 9].Style.Numberformat.Format = "###,###,##0";
				Sheet.Cells[iRow, 10].Value = fPriceLowP;
				Sheet.Cells[iRow, 10].Style.Numberformat.Format = "###,###,##0.00";
				if(fPriceLowP > 0)
					Sheet.Cells[iRow, 10].Style.Font.Color.SetColor(Color.Red);
				else if(fPriceLowP < 0)
					Sheet.Cells[iRow, 10].Style.Font.Color.SetColor(Color.Blue);
				Sheet.Cells[iRow, 11].Value = iPriceHigh;
				Sheet.Cells[iRow, 11].Style.Numberformat.Format = "###,###,##0";
				Sheet.Cells[iRow, 12].Value = fPriceHighP;
				Sheet.Cells[iRow, 12].Style.Numberformat.Format = "###,###,##0.00";
				if(fPriceHighP > 0)
					Sheet.Cells[iRow, 12].Style.Font.Color.SetColor(Color.Red);
				else if(fPriceHighP < 0)
					Sheet.Cells[iRow, 12].Style.Font.Color.SetColor(Color.Blue);
				Sheet.Cells[iRow, 13].Value = strLink;
				

				Sheet.Cells.AutoFitColumns(0);
				package.Save();
			}

			return true;
		}
	}
}
