// MainDlg.cpp : implementation of the CLoginDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LoginDlg.h"
#include "StabtypeControl.h"
#include "CApp.h"
#include "CNetWork.h"
	
CLoginDlg::CLoginDlg() : SHostWnd(_T("LAYOUT:XML_LOGIN"))
{
	m_bLayoutInited = FALSE;
	m_is_reg = FALSE;
	 
}

CLoginDlg::~CLoginDlg()
{
	 
}

int CLoginDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	SetMsgHandled(FALSE);
	return 0;
}

BOOL CLoginDlg::OnInitDialog(HWND hWnd, LPARAM lParam)
{
	m_bLayoutInited = TRUE;
	Init();
	return 0;
}

void CLoginDlg::Init()
{

	//if (m_network_tools == nullptr)
	//{
	//	m_network_tools = new CManageNetWork();
	//	//m_network_tools.Attach(new CManageNetWork());
	//	m_network_tools->init();
	//}

	CManageNetWork::getSingletonPtr()->init();
 
	



	ServerListUrl list;
	list.id = 0;
	list.name = L"首选";
	list.url = "https://api.ruanjian.buzz";

	ServerListUrl list1;
	list1.id = 1;
	list1.name = L"备用1";
	list1.url = "https://api.ruanjian1.buzz";

	ServerListUrl list2;
	list2.id = 2;
	list2.name = L"备用2";
	list2.url = "https://ruanjian2.xyz";


	vctInfo.push_back(list);
	vctInfo.push_back(list1);
	vctInfo.push_back(list2);

	vctInfoUrl.push_back(list);
	vctInfoUrl.push_back(list2);
	vctInfoUrl.push_back(list2);

	CManageNetWork::getSingletonPtr()->SetUrlList(vctInfo);
	StabtypeControl* tabtype = FindChildByName2<StabtypeControl>(L"nodeclass");
	if (tabtype)
	{
		for (auto i = 0; i < vctInfo.size(); i++)
		{
			tabtype->ItemCreateChildren(vctInfo[i].id, vctInfo[i].name, i == 0 ? true : false);

		}
		if (vctInfo.size() > 0)
		{
			CManageNetWork::getSingletonPtr()->SetUrl(vctInfo[0].url.c_str());
			 
			//Curl::getSingletonPtr()->SetUrl(vctInfo[0].url);
		}

		tabtype->GetEventSet()->subscribeEvent(EVT_TABTYPE_CONTROL,
			Subscriber(&CLoginDlg::OnTabtypeControl, this));
	}

	SEdit* m_edit_username = FindChildByName2<SEdit>(L"edit_username");
	SEdit* m_edit_password = FindChildByName2<SEdit>(L"edit_password");
	if (m_edit_username) {
		m_edit_username->SetWindowTextW(S_CA2W(CManageNetWork::getSingletonPtr()->GetUsername()));
		m_edit_username->Invalidate();
	}

	if (m_edit_password)
	{
		m_edit_password->SetWindowTextW(S_CA2W(CManageNetWork::getSingletonPtr()->GetPassWord()));
		m_edit_password->Invalidate();
	}

	SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
	if (onlogin)
	{
		onlogin->SetWindowTextW(L"正在获取系统配置..");
		onlogin->EnableWindow(FALSE);
		onlogin->Invalidate();
	}

	CManageNetWork::getSingletonPtr()->GetSysConfig();
	
	/*CManageNetWork::getSingletonPtr()->DonloadConfg();*/
}
bool CLoginDlg::OnTabtypeControl(SOUI::EventArgs* pEvt)
{
	SOUI::EventTabtypeControl* pTestControlEvent =
		SOUI::sobj_cast<SOUI::EventTabtypeControl>(pEvt);

	CManageNetWork::getSingletonPtr()->SetUrl(vctInfo[pTestControlEvent->nIndex].url.c_str());
	
	/*if (m_curl)
	{
		m_curl->SetUrl(vctInfo[pTestControlEvent->nIndex].url.c_str());
	}*/

	//Curl::getSingletonPtr()->SetUrl(vctInfo[pTestControlEvent->nIndex].url);
	return true;
}
//TODO:消息映射
void CLoginDlg::OnClose()
{
	CSimpleWnd::DestroyWindow();
}

void CLoginDlg::OnMaximize()
{
	SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
}
void CLoginDlg::OnRestore()
{
	SendMessage(WM_SYSCOMMAND, SC_RESTORE);
}
void CLoginDlg::OnMinimize()
{
	SendMessage(WM_SYSCOMMAND, SC_MINIMIZE);
}


void CLoginDlg::OnLogin()
{
 

	SEdit* m_edit_username = FindChildByName2<SEdit>(L"edit_username");
	SEdit* m_edit_password = FindChildByName2<SEdit>(L"edit_password");
 	if (m_edit_username && m_edit_password)
	{
		auto username = m_edit_username->GetWindowTextW();
		auto password = m_edit_password->GetWindowTextW();


		/*if (m_base_curl) {
			char data[256] = { 0 };

			HTTPRET httpret = m_base_curl->PostLogin(S_CW2A(username).GetBuffer(0), S_CW2A(password).GetBuffer(0), data);
			SLOG_DEBUG(data);
		}*/
		if (m_is_reg == FALSE)
		{
			CManageNetWork::getSingletonPtr()->Login(S_CW2A(username).GetBuffer(0), S_CW2A(password).GetBuffer(0));
		}
		else {
			CManageNetWork::getSingletonPtr()->Reg(S_CW2A(username).GetBuffer(0), S_CW2A(password).GetBuffer(0));
		}
	}

	SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
	if (onlogin)
	{
		auto text = m_is_reg == FALSE ? L"正在登录..." : L"正在注册...";
		onlogin->SetWindowTextW(text);
		onlogin->EnableWindow(FALSE);
		onlogin->Invalidate();
	}
	/**/

}

void CLoginDlg::ToMain()
{
	CApp::getSingletonPtr()->SetOut(1);
	CSimpleWnd::DestroyWindow();
}

void CLoginDlg::OnUpdateLInkUI()
{
	/*SLink* reglink = FindChildByName2<SLink>(L"reglink");
	if (reglink)
	{
		reglink->SetAttribute(L"href", S_CA2W(CApp::getSingletonPtr()->GetSysconfig()->user_reg.c_str(),CP_UTF8));
		reglink->UpdateWindow();
	}*/
	
	SLink* resetlink = FindChildByName2<SLink>(L"resetlink");
	if (resetlink)
	{
		resetlink->SetAttribute(L"href", S_CA2W(CApp::getSingletonPtr()->GetSysconfig()->user_reset.c_str(), CP_UTF8));
		resetlink->UpdateWindow();
	}

}

void CLoginDlg::OnLoginFinish(EventArgs* e)
{
	EventLogin* e2 = sobj_cast<EventLogin>(e);
	if (!e2)
	{
		return;
	}

	if (e2->status == 0)
	{
		SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
		if (onlogin)
		{
			onlogin->SetWindowTextW(e2->msg);
			onlogin->EnableWindow(TRUE);
			onlogin->Invalidate();
		}

		SMessageBox(m_hWnd, e2->msg, L"提示", 0);

	} else if (e2->status == -1)
	{
		SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
		if (onlogin)
		{
			onlogin->SetWindowTextW(e2->msg);
			onlogin->EnableWindow(TRUE);
			onlogin->Invalidate();
		}

		SMessageBox(m_hWnd, e2->msg, L"提示", 0);
		::ShellExecute(NULL, _T("open"), CApp::getSingletonPtr()->GetSysconfig()->GetBuyLink(), NULL, NULL, SW_SHOWNORMAL);
	} 
	else if (e2->status == -2) //用户到期
	{
		SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
		if (onlogin)
		{
			onlogin->SetWindowTextW(e2->msg);
			onlogin->EnableWindow(TRUE);
			onlogin->Invalidate();
		}

		SMessageBox(m_hWnd, e2->msg, L"提示", 0);

		::ShellExecute(NULL, _T("open"), CApp::getSingletonPtr()->GetSysconfig()->GetBuyLink(), NULL, NULL, SW_SHOWNORMAL);
	}
	else if (e2->status == -3) //用户流量用完
	{
		
		SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
		if (onlogin)
		{
			onlogin->SetWindowTextW(e2->msg);
			onlogin->EnableWindow(TRUE);
			onlogin->Invalidate();
		}

		SMessageBox(m_hWnd, e2->msg, L"提示", 0);

		::ShellExecute(NULL, _T("open"), CApp::getSingletonPtr()->GetSysconfig()->GetOnlineLink(), NULL, NULL, SW_SHOWNORMAL);
	}
	else if (e2->status == 200)
	{
		/*if (m_network_tools)
		{
			m_network_tools->DonloadConfg();
		}*/

		ToMain();

	}


	
}

void CLoginDlg::OnDonloadFinish(EventArgs* e)
{
	EventDoWNload* e2 = sobj_cast<EventDoWNload>(e);
	if (e2)
	{
		if (e2->status != 200)
		{
			SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
			if (onlogin)
			{
				onlogin->SetWindowTextW(e2->msg);
				onlogin->EnableWindow(TRUE);
				onlogin->Invalidate();
			}
			SMessageBox(m_hWnd, e2->msg, L"提示", 0);
		}
		else {
			ToMain();
		}
	}
}

void CLoginDlg::OnSysConfig(EventArgs* e)
{
	EventSysconfig* e2 = sobj_cast<EventSysconfig>(e);
	if (e2)
	{
		if (e2->status == 200)
		{
			SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
			if (onlogin)
			{
				onlogin->SetWindowTextW(L"获取配置成功,点击登录");
				onlogin->EnableWindow(TRUE);
				onlogin->Invalidate();
			}

			OnUpdateLInkUI();
		
		}
		else {
			SMessageBox(m_hWnd, e2->msg, L"提示", 0);
		}
	}
}

void CLoginDlg::OnReg()
{
	if (m_is_reg == FALSE)
	{
		SLink* resetlink = FindChildByName2<SLink>(L"resetlink");
		if (resetlink)
		{
			resetlink->SetAttribute(L"show", L"0");
			resetlink->Invalidate();
		}

		SLink* reglink = FindChildByName2<SLink>(L"reglink");
		if (reglink)
		{
			reglink->SetWindowTextW(L"切换登录界面");
			///reglink->SetAttribute(L"show", L"0");
			reglink->Invalidate();
		}

		SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
		if (onlogin)
		{
			onlogin->SetWindowTextW(L"点击注册");
			onlogin->EnableWindow(TRUE);
			onlogin->Invalidate();
		}

		SEdit* m_edit_username = FindChildByName2<SEdit>(L"edit_username");
		SEdit* m_edit_password = FindChildByName2<SEdit>(L"edit_password");
		if (m_edit_username && m_edit_password)
		{
			/*auto username = m_edit_username->GetWindowTextW();
			auto password = m_edit_password->GetWindowTextW();*/
			m_edit_username->SetWindowTextW(L"");
			m_edit_password->SetWindowTextW(L"");
		}

		m_is_reg = TRUE;
	}
	else 
	{
		SLink* resetlink = FindChildByName2<SLink>(L"resetlink");
		if (resetlink)
		{
			resetlink->SetAttribute(L"show", L"1");
			resetlink->Invalidate();
		}

		SLink* reglink = FindChildByName2<SLink>(L"reglink");
		if (reglink)
		{
			reglink->SetWindowTextW(L"注册新用户");
			///reglink->SetAttribute(L"show", L"0");
			reglink->Invalidate();
		}

		SEdit* m_edit_username = FindChildByName2<SEdit>(L"edit_username");
		SEdit* m_edit_password = FindChildByName2<SEdit>(L"edit_password");
		if (m_edit_username) {
			m_edit_username->SetWindowTextW(S_CA2W(CManageNetWork::getSingletonPtr()->GetUsername()));
			m_edit_username->Invalidate();
		}

		if (m_edit_password)
		{
			m_edit_password->SetWindowTextW(S_CA2W(CManageNetWork::getSingletonPtr()->GetPassWord()));
			m_edit_password->Invalidate();
		}

		SImageButton* onlogin = FindChildByName2<SImageButton>(L"onlogin");
		if (onlogin)
		{
			onlogin->SetWindowTextW(L"点击登录");
			onlogin->EnableWindow(TRUE);
			onlogin->Invalidate();
		}

		m_is_reg = FALSE;
	}


	
}
