#include <string>
#include <stdio.h>
#include <windows.h>
#include <stdexcept>
#include "excel.h"

using namespace std;
using namespace libxl;

void setKey( libxl::Book * pBook )
{
	if ( pBook != nullptr )
	{
		pBook->setKey( L"cpic", L"windows-202d21040bc4e70060bc6264a6ucu7i1" );
	}
}

void setKey( libxl::IBookT<char> * pBook )
{
	if (pBook != nullptr)
	{
		pBook->setKey("cpic", "windows-202d21040bc4e70060bc6264a6ucu7i1");
	}
}

std::wstring ReadCellStringFromXlsx( libxl::IBookT<wchar_t> * pBook,
                                     unsigned int             sheetIndex,
                                     unsigned int             rowIndex,
                                     unsigned int             colIndex,
                                     bool                     isInteger )
{
	using namespace libxl;

	std::wstring returnValue;

	int year   = 0;
	int month  = 0;
	int day    = 0;
	int hour   = 0;
	int min    = 0;
	int second = 0;

	//验证
	if ( pBook == nullptr )
	{
		throw std::logic_error( "ReadCellStringFromXlsx pSheet参数错误！" );
	}

	ISheetT<wchar_t> * pSheet   = pBook->getSheet( sheetIndex );
	CellType           cellType = pSheet->cellType( rowIndex, colIndex );

	//判断单元格类型
	switch ( cellType )
	{
		case CellType::CELLTYPE_BOOLEAN: //bool类型
			{
				returnValue = pSheet->readBool( rowIndex, colIndex ) ? L"true" : L"false";

				break;
			}
		case CellType::CELLTYPE_NUMBER: //数字类型，还得再判断一下是不是日期类型
			{
				double  cellValue = pSheet->readNum( rowIndex, colIndex );
				wchar_t buffer[50]; //生成字符串的缓冲区

				if ( pSheet->isDate( rowIndex, colIndex ) == true )
				{
					//是日期类型
					if ( pBook->dateUnpack( cellValue,
					                        &year,
					                        &month,
					                        &day,
					                        &hour,
					                        &min,
					                        &second ) == false )
					{
						throw std::runtime_error( "转换日期格式失败！" );
					}

					//排除bug
					if ( min >= 60 )
					{
						hour = hour + 1;
						min  = min - 60;
					}

					// if ( hour >= 24 )
					// {
					// 	day = day + 1;
					// 	hour = hour - 24;
					// }
					//
					// switch ( month )
					// {
					// case 1:
					// case 3:
					// case
					// }

					wsprintfW( buffer, L"%d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, day, hour, min, second );
				}
				else
				{
					//是数字类型, 根据isInteger参数选择输出整型或者浮点型
					if ( isInteger == true )
					{
						wsprintfW( buffer, L"%d", static_cast<long>(cellValue) );
					}
					else
					{
						wsprintfW( buffer, L"%f", cellValue );
					}
				}

				returnValue = buffer;

				break;
			}
		case CELLTYPE_STRING: //字符串类型，要先判断一下读取的结果是不是空值
			{
				const wchar_t * pValue = pSheet->readStr( rowIndex, colIndex );

				if ( pValue != nullptr )
				{
					returnValue = pValue;
				}

				break;
			}
	}

	return returnValue;
}

libxl::Sheet * getXlsxSheetByName( libxl::IBookT<wchar_t> * pBook, const std::wstring & sheetName )
{
	Sheet * pSheet        = nullptr;
	Sheet * pCurrentSheet = nullptr;

	if ( pBook == nullptr )
	{
		throw logic_error( "参数错误！" );
	}

	int sheetCount = pBook->sheetCount();
	int sheetIndex = 0;

	while ( sheetIndex < sheetCount )
	{
		pCurrentSheet = pBook->getSheet( sheetIndex );

		wstring currentSheetName = pCurrentSheet->name();

		if ( currentSheetName == sheetName )
		{
			pSheet = pCurrentSheet;

			break;
		}

		sheetIndex++;
	}

	return pSheet;
}
