using namespace std;

#include "stdafx.h"
#include <afxwin.h>
#include <afxext.h>
#include <afxdisp.h>
#include <afxdtctl.h>
#include <afxcmn.h>
#include <iostream>

#include "CmdParser.h"

CCommandParser::CCommandParser(void) {}

BOOL is_valid_param(LPWSTR argv, WCHAR *param_short, WCHAR *param_long=L"")
{
	if (_wcsicmp(CString(argv).MakeLower(), param_short) == 0 ||
		_wcsicmp(CString(argv).MakeLower(), param_long) == 0) {return true;}
	return false;
}

void CCommandParser::print_params()
{
	wcout << L"\n�ֿ����:" << endl;
	wcout << L"      ���� : " << fontface.GetString() << endl;
	wcout << L"      ��С : " << font_size << endl;
	wcout << L"      ���� : " << (font_weight == 400 ? L"����" : L"����") << endl;
	wcout << L"      � : " << (font_itatlic == 0 ? L"��" : L"��") << endl;
	wcout << L"      ���� : " << font_width << endl;
	wcout << L"      �߶� : " << font_height << endl;
	wcout << L"  ˮƽƫ�� : " << font_offset_x << endl;
	wcout << L"  ��ֱƫ�� : " << font_offset_y << endl;
	wcout << L"  ɨ��ģʽ : " << (scan_mode == 0 ? L"ˮƽ" : L"��ֱ") << endl;
	wcout << L"  �ֽ�˳�� : " << (byte_order == 0 ? L"��λ��ǰ" : L"��λ��ǰ") << endl;
	wcout << L"    ������ : " << (has_index_table == 0 ? L"��" : L"��") << endl;
	wcout << L"  �ַ����� : " << (char_width == 0 ? L"�̶�" : L"�ɱ�") << endl;
	wcout << L"  ����ļ� : " << output_file.GetString() << "\n" << endl;
}

void CCommandParser::parse(void)
{
	argv_list = CommandLineToArgvW(GetCommandLineW(), &args);

	if (!argv_list || args == 1) {
		print_help();
		exit(0);
	}

	for (int index = 1; index < args; index++) {
		if (is_valid_param(argv_list[index], L"-f", L"--face")) {
			fontface = CString(argv_list[index + 1]);
			index++;
		} else if (is_valid_param(argv_list[index], L"-s", L"--size")) {
			font_size = _wtoi(argv_list[index + 1]);
			font_width = font_size;
			font_height = font_size;
			index++;
		} else if (is_valid_param(argv_list[index], L"-n", L"--normall")) {
			font_weight = 400;
		} else if (is_valid_param(argv_list[index], L"-b", L"--bold")) {
			font_weight = 700;
		} else if (is_valid_param(argv_list[index], L"-i", L"--italic")) {
			font_itatlic = 1;
		} else if (is_valid_param(argv_list[index], L"-x", L"--offset-x")) {
			font_offset_x = _wtoi(argv_list[index + 1]);
			index++;
		} else if (is_valid_param(argv_list[index], L"-y", L"--offset-y")) {
			font_offset_y = _wtoi(argv_list[index + 1]);
			index++;
		} else if (is_valid_param(argv_list[index], L"-sh", L"--scan-horizontal")) {
			scan_mode = SCAN_MODE_HORIZONTAL;
		} else if (is_valid_param(argv_list[index], L"-sv", L"--scan-vertical")) {
			scan_mode = SCAN_MODE_VERTICAL;
		} else if (is_valid_param(argv_list[index], L"-m", L"--msb")) {
			byte_order = BYTEORDER_MSB;
		} else if (is_valid_param(argv_list[index], L"-l", L"--lsb")) {
			byte_order = BYTEORDER_LSB;
		} else if (is_valid_param(argv_list[index], L"-o", L"--output")) {
			output_file = CString(argv_list[index + 1]);
			index++;
		} else if (is_valid_param(argv_list[index], L"", L"--input")) {
			input_customized_file = CString(argv_list[index + 1]);
			using_customized_file = 1;
			index++;
		} else if (is_valid_param(argv_list[index], L"", L"--charset")) {
			input_customized_cst = CString(argv_list[index + 1]);
			using_customized_cst = 1;
			index++;
		} else if (is_valid_param(argv_list[index], L"", L"--version")) {
			wcout << "Font Maker Client " << app_version.GetString() << endl;
			exit(0);
		}
	}

	LocalFree(argv_list);

	if (fontface.GetLength() == 0) {
		wcout << L"�����ļ�����Ϊ�գ�" << endl;
		exit(1);
	}

	if (scan_mode == SCAN_MODE_VERTICAL && byte_order == BYTEORDER_MSB) {
		wcout << L"��֧�� VMSB ģʽ��" << endl;
		exit(1);
	}
}

void CCommandParser::print_help(void) {
	char filename[MAX_PATH + 1];
	strcpy_s(filename, CT2A(get_exe_filename().GetBuffer()));

	wcout << "Usage: " << filename << " [OPTIONS]" << endl;
	wcout << "" << endl;
	wcout << "Options:" << endl;
	wcout << "    -f, --face FONT_FACE"			<< L"	�������ơ��硰��Բ������Consolas����[�����]\n" << endl;
	wcout << "    -s, --size FONT_SIZE"			<< L"	�����С���硰16������24����[Ĭ��ֵ��16]\n" << endl;
	wcout << "    -n, --normal"				<< L"		��������Ϊ����ͨ��[Ĭ��ֵ]\n" << endl;
	wcout << "    -b, --bold"			<< L"			��������Ϊ���Ӵ�\n" << endl;
	wcout << "    -i, --italic"				<< L"		������ʾΪб��\n" << endl;
	wcout << "    -x, --offset-x OFFSETX"		<< L"	�ַ�ˮƽƫ�������硰3������-5����[Ĭ��ֵ��0]\n" << endl;
	wcout << "    -y, --offset-y OFFSETY"		<< L"	�ַ���ֱƫ�������硰-2������3����[Ĭ��ֵ��0]\n" << endl;
	wcout << "    -sv --scan-vertical"		<< L"		��ֱ��ʽɨ���ַ�����[Ĭ��ֵ]\n" << endl;
	wcout << "    -sh --scan-horizontal"		<< L"	ˮƽ��ʽɨ���ַ�����\n" << endl;
	wcout << "    -l, --lsb" << L"			�ֽڵ�λ��ǰ��[Ĭ��ֵ]\n" << endl;
	wcout << "    -m, --msb" << L"			�ֽڸ�λ��ǰ��\n" << endl;
	wcout << "    --input FILENAME" << L"		ָ�������ı��������Զ����ֿ⡣\n" << endl;
	wcout << "    --charset FILENAME" << L"		ָ���ַ����ļ��������Զ����ֿ⡣\n" << endl;
	wcout << "    -o, --output FILENAME"		<< L"	ָ�������ļ����ļ�����[Ĭ��ֵ��combined.bin]\n" << endl;
	wcout << "\nDisabled Options:" << endl;
	wcout << "    -w, --width WIDTH" << L"		�ַ����ȡ��硰16������24����[�����Ĭ��ֵ��FONT_SIZE]\n" << endl;
	wcout << "    -h, --height HEIGHT" << L"		�ַ��߶ȡ��硰16������24����[�����Ĭ��ֵ��FONT_SIZE]\n" << endl;
	wcout << "    -d, --fixed-width" << L"		�ַ����ȹ̶���[�����Ĭ��ֵ]\n" << endl;
	wcout << "    -v, --variable-width" << L"	�ַ����ȿɱ䡣[������]\n" << endl;
	wcout << "    -t, --include-table" << L"		���� GB2312 ��������[�����Ĭ��ֵ]\n" << endl;
	wcout << "\nSamples:" << endl;
	wcout << "    " << filename << L" -f ��Բ" << L"				ʹ��Ĭ�ϲ���ֵ������Բ������ֿ�\n" << endl;
	wcout << "    " << filename << L" -f \"΢���ź�\" -s 24 -o msyh.bin" << L"	����΢���ź����壬�ֺ� 24����ͨ���أ���ֱɨ�裬" << endl;
	wcout << L"							�ֽڵ�λ��ǰ���ļ��� msyh.bin ���ֿ��ļ�\n" << endl;
	wcout << "    " << filename << L" -f ��Բ --input novel.txt" << L"		���� novel.txt �е����������Զ����ֿ�" << endl;
}

CString CCommandParser::get_exe_filename(void) {
	TCHAR full_path[MAX_PATH + 1];
	CString csFileFullName;

	GetModuleFileName(NULL, full_path, MAX_PATH);

	csFileFullName = full_path;
	int nPos = csFileFullName.ReverseFind('\\');
	
	csFileFullName = csFileFullName.Right(csFileFullName.GetLength() - nPos - 1);
	return csFileFullName;
}