send_campaign = {}

send_campaign.app = sf.core.g_Application

send_campaign.strings = {}
send_campaign.strings[0] = _T("%d %%")--sf.misc.g_StringTable.Instance().FormatByStringsID(_T("$559"))
send_campaign.strings[1] = sf.misc.g_StringTable.Instance().FormatByStringsID(_T("$354"))
send_campaign.strings[2] = sf.misc.g_StringTable.Instance().FormatByStringsID(_T("$337"))
send_campaign.strings[3] = sf.misc.g_StringTable.Instance().FormatByStringsID(_T("$348"))

send_campaign.enum = {}
send_campaign.enum.connection_failed = 0
send_campaign.enum.transfer_failed = 1
send_campaign.enum.paused = 2
send_campaign.enum.cancelled = 3
send_campaign.enum.successful = 4

send_campaign.min_count_levels = 10

send_campaign.campaign_name = _T("")
send_campaign.campaign_file_crc = 0

send_campaign.server = send_campaign.app.GetSettings().GetChild(_T("settings"), false).GetValue(_T("send_campaign_server"))
send_campaign.recipient = send_campaign.app.GetSettings().GetChild(_T("settings"), false).GetValue(_T("send_campaign_recipient"))

send_campaign.options = s4.g_GameOptions.Instance().GetSendingOptions()

send_campaign.campaign_nums = {}

function send_campaign.LoadWindow(_owner, _window, _id)

	_window.Load(	send_campaign.app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _id, true),
					send_campaign.app.GetSettings().GetChild(_T("GUITemplates"), false))
	
	_window.UpdateLayout()
	
	_window.SetOffset(	(_owner.GetSize().X - _window.GetSize().X) / 2.0,
						(_owner.GetSize().Y - _window.GetSize().Y) / 2.0)

end

function send_campaign.GetLuaTime(_time)
	
	return os.time{year=_time.wYear, month=_time.wMonth, day=_time.wDay, 
					hour=_time.wHour, min=_time.wMinute, sec=_time.wSecond}
end

send_campaign.on_child_action = {}

send_campaign.on_child_action.s4_sending_connection = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("cancel") then _owner.EndModal(1) end
		if _child.GetId() == _T("continue") then _owner.EndModal(0) end
		
	end

end

send_campaign.on_child_action.s4_sending = function(_owner, _action, _child)

	if _action == _T("pressed") then

		local smtp_client = __cast(_owner.GetWidget(_T("smtp_client")), sf.gui.CSMTPClient)
		smtp_client.Pause(true)
		
		_owner.EndModal(send_campaign.enum.paused)
	
	elseif _action == _T("changed") then

		local label_text = __cast(_owner.GetWidget(_T("label_text")), sf.gui.CLabelWidget);
		local label_progress = __cast(_owner.GetWidget(_T("label_progress")), sf.gui.CLabelWidget);
		local progress_bar = __cast(_owner.GetWidget(_T("progress")), sf.gui.CProgressBarWidget);
		local smtp_client = __cast(_child, sf.gui.CSMTPClient)
		local state = smtp_client.GetState()
		
		local text = _T(string.format(T_(send_campaign.strings[2]).c_str(), T_(send_campaign.campaign_name).c_str()))
		local progress = (100 * smtp_client.GetProgress())
		local progress_text = _T(string.format(T_(send_campaign.strings[0]).c_str(), progress))

		label_text.SetText(text)
		label_progress.SetText(progress_text)
		progress_bar.SetValue(smtp_client.GetProgress())
		
		_owner.UpdateLayout()
		
		if state == sf.gui.CSMTPClient.StateConnectingFailed then
		
			_owner.EndModal(send_campaign.enum.connection_failed)
		
		elseif state == sf.gui.CSMTPClient.StateTransferringFailed then
		
			_owner.EndModal(send_campaign.enum.transfer_failed)
		
		elseif state == sf.gui.CSMTPClient.StateTransferringSuccessful then
			
			_owner.EndModal(send_campaign.enum.successful)
			
		end
		
	end

end

send_campaign.on_child_action.s4_sending_disconnect = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("cancel") then _owner.EndModal(1) end
		if _child.GetId() == _T("continue") then _owner.EndModal(0) end
		
	end

end

send_campaign.on_child_action.s4_sending_cancel = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("no") then _owner.EndModal(0) end
		if _child.GetId() == _T("yes") then _owner.EndModal(1) end
		
	end

end

send_campaign.on_child_action.s4_sending_complete = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
		
	end

end

send_campaign.on_child_action.s4_sending_already = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
		
	end

end

send_campaign.on_child_action.s4_sending_not_name_found = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
		
	end

end

send_campaign.on_child_action.s4_sending_no_select_campaign = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
		
	end

end

send_campaign.is_english = function(_string)

	local len = string.len(_string)

	for i=1, len do
		local code = string.byte(_string, i)
		if code<32 or code>126 then 
			return false 
		end 
	end
		
	return true
	
end

send_campaign.on_child_action.s4_sending_campaign = function(_owner, _action, _child)

	if _action == _T("overed") or _action == _T("lost_over") then
		return
	end

	local button_send = __cast(_owner.GetWidget(_T("send")), sf.gui.CButtonWidget)
	local combobox_campaigns = __cast(_owner.GetWidget(_T("campaign_list")), "sf.gui.CComboBoxWidget")
	local edit_name = __cast(_owner.GetWidget(_T("send_edit")), "sf.gui.CEditWidget")
	local edit_mail = __cast(_owner.GetWidget(_T("edit_mail")), "sf.gui.CEditWidget")
	local edit_description = __cast(_owner.GetWidget(_T("description_of_campaign")), "sf.gui.CEditWidget")

	button_send.DisableButton(combobox_campaigns.GetSelectedLine()==-1 or edit_name.GetText()==_T(""))
	
	if _action == _T("pressed") then
		
		if _child.GetId() == _T("cancel") then
		
			_owner.EndModal(0)
			
		elseif _child.GetId() == _T("send") then
					
			local campaign = s4.le.g_Campaigns.Instance().GetCampaign(send_campaign.campaign_nums[combobox_campaigns.GetSelectedLine()])
						
			local name = T_(edit_name.GetText()).c_str()
			local mail = T_(edit_mail.GetText()).c_str()
			local description = T_(edit_description.GetText()).c_str()
				
			if send_campaign.is_english(name)==false or send_campaign.is_english(description)==false or send_campaign.is_english(mail)==false then
			
				local dialog_need_english = sf.gui.CWindow()
				send_campaign.LoadWindow(_owner, dialog_need_english, _T("s4_sending_need_english"))
				local dialog_need_english_ptr = sf.gui.CBaseWindowPtrT(dialog_need_english)
							
				_owner.DoModal(dialog_need_english_ptr)			
				
			elseif campaign.EnumLevels() < send_campaign.min_count_levels then
			
				local dialog_few_levels = sf.gui.CWindow()
				send_campaign.LoadWindow(_owner, dialog_few_levels, _T("s4_sending_few_levels"))
				local dialog_few_levels_ptr = sf.gui.CBaseWindowPtrT(dialog_few_levels)
				
				local label_text = __cast(dialog_few_levels.GetWidget(_T("label_text")), sf.gui.CLabelWidget)
				label_text.SetText(_T(string.format(T_(label_text.GetText()).c_str(), send_campaign.min_count_levels)))
				
				_owner.DoModal(dialog_few_levels_ptr)			
			
			else
				
				campaign.Save(false)
				send_campaign.campaign_file_crc = sf.misc.CalcFileCRC(campaign.GetFilePath())
				
				if send_campaign.options.IsSendedCampaign(send_campaign.campaign_file_crc)==true then
			
					local dialog_few_levels = sf.gui.CWindow()
					send_campaign.LoadWindow(_owner, dialog_few_levels, _T("s4_sending_already"))
					local dialog_few_levels_ptr = sf.gui.CBaseWindowPtrT(dialog_few_levels)
					_owner.DoModal(dialog_few_levels_ptr)
			
				else
				
					_owner.EndModal(1)
				
				end
				
			end
			
		end
		
	end

end

send_campaign.on_child_action.s4_sending_few_levels = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
		
	end

end

send_campaign.on_child_action.s4_sending_need_english = function(_owner, _action, _child)

	if _action == _T("pressed") then
		
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
		
	end

end

function init_dialog_prepare_sending_campaign(_dialog)
	
	local combobox_campaigns = __cast(_dialog.GetWidget(_T("campaign_list")), "sf.gui.CComboBoxWidget")
	local edit_name = __cast(_dialog.GetWidget(_T("send_edit")), "sf.gui.CEditWidget")
	local edit_description = __cast(_dialog.GetWidget(_T("description_of_campaign")), "sf.gui.CEditWidget")
	
	edit_name.SetText(send_campaign.strings[1])
	
	local camp_i = 0 
	local camp_count = s4.le.g_Campaigns.Instance().EnumCampaigns()
	local line_i = 0
	
	while camp_i < camp_count do
	
		local campaign = s4.le.g_Campaigns.Instance().GetCampaign(camp_i)		

		if sf.misc.BitwiseAnd(campaign.GetFlags(), s4.le.CCampaign.FlagEditable) ~= 0 then
			combobox_campaigns.AddLine(campaign.GetId())
			send_campaign.campaign_nums[line_i] = camp_i
			line_i = line_i+1
		end

		camp_i = camp_i+1
	
	end

end

function send_campaign.try(_owner)

	local dialog_prepare_sending_campaign = sf.gui.CWindow()
	local dialog_prepare_sending_campaign_ptr = sf.gui.CBaseWindowPtrT(dialog_prepare_sending_campaign)
	
	send_campaign.LoadWindow(_owner, dialog_prepare_sending_campaign, _T("s4_sending_campaign"))
	
	init_dialog_prepare_sending_campaign(dialog_prepare_sending_campaign)
	
	if _owner.DoModal(dialog_prepare_sending_campaign_ptr) == 1 then
	
		local combobox_campaigns = __cast(dialog_prepare_sending_campaign.GetWidget(_T("campaign_list")), "sf.gui.CComboBoxWidget")
		local edit_name = __cast(dialog_prepare_sending_campaign.GetWidget(_T("send_edit")), "sf.gui.CEditWidget")
		local edit_mail = __cast(dialog_prepare_sending_campaign.GetWidget(_T("edit_mail")), "sf.gui.CEditWidget")
		local edit_description = __cast(dialog_prepare_sending_campaign.GetWidget(_T("description_of_campaign")), "sf.gui.CEditWidget")
		
		local campaign = s4.le.g_Campaigns.Instance().GetCampaign(send_campaign.campaign_nums[combobox_campaigns.GetSelectedLine()])
		send_campaign.campaign_name = campaign.GetId()
		local profile = s4.g_ProfileManager.Instance().GetCurrentProfile()
					
		local smtp_client = sf.gui.CSMTPClient(_T("smtp_client"))
		local smtp_client_ptr1 = sf.gui.CWidgetPtrT(smtp_client)
	
		local server = send_campaign.server
		local recipient = send_campaign.recipient
		local sender = edit_name.GetText()
		local subject = _T("campaign")
		local body = _T("")
		
		body = body + _T("author_name=\"") + edit_name.GetText() + _T("\"\n")
		body = body + _T("author_mail=\"") + edit_mail.GetText() + _T("\"\n")
		body = body + _T("author_guid=\"")
		if profile ~= nil then body = body + profile.GetGUID() end
		body = body + _T("\"\n")
		body = body + _T("description=\"") + edit_description.GetText() + _T("\"\n")
		
		local sending_result = send_campaign.enum.paused	
		
		while sending_result ~= send_campaign.enum.successful and sending_result ~= send_campaign.enum.cancelled do
					
			sending_result = send_campaign.enum.paused
						
			while sending_result == send_campaign.enum.paused do
		
				local dialog_sending = sf.gui.CWindow()
				local dialog_sending_ptr = sf.gui.CBaseWindowPtrT(dialog_sending)	
				send_campaign.LoadWindow(_owner, dialog_sending, _T("s4_sending"))
				local smtp_client_ptr2 = sf.gui.CWidgetPtrT(smtp_client)
				smtp_client.RemFlags(sf.gui.CBaseWidget.FlagDead)
				dialog_sending.AddWidget(smtp_client_ptr2)
		
				smtp_client.Send(server, recipient, sender, subject, body, campaign.GetFilePath());
				
				sending_result = _owner.DoModal(dialog_sending)
				
				if sending_result == send_campaign.enum.paused then
					
					local dialog_cancel = sf.gui.CWindow()
					send_campaign.LoadWindow(_owner, dialog_cancel, _T("s4_sending_cancel"))
					local dialog_cancel_ptr = sf.gui.CBaseWindowPtrT(dialog_cancel)
					
					if _owner.DoModal(dialog_cancel) == 1 then
					
						smtp_client.Abort()
						sending_result = send_campaign.enum.cancelled
						
					else
					
						smtp_client.Pause(false)
					
					end
				
				end
			
			end
			
			if sending_result == send_campaign.enum.connection_failed then
			
				local dialog_connection_error = sf.gui.CWindow()
				send_campaign.LoadWindow(_owner, dialog_connection_error, _T("s4_sending_connection"))
				local dialog_connection_error_ptr = sf.gui.CBaseWindowPtrT(dialog_connection_error)
				
				if _owner.DoModal(dialog_connection_error_ptr) == 1 then 
					sending_result = send_campaign.enum.cancelled 
				end
		
			elseif sending_result == send_campaign.enum.transfer_failed then
			
				local dialog_transfer_error = sf.gui.CWindow()
				send_campaign.LoadWindow(_owner, dialog_transfer_error, _T("s4_sending_disconnect"))
				local dialog_transfer_error_ptr = sf.gui.CBaseWindowPtrT(dialog_transfer_error)
				
				if _owner.DoModal(dialog_transfer_error_ptr) == 1 then 
					sending_result = send_campaign.enum.cancelled 
				end
		
			end
		
		end
		
		if sending_result == send_campaign.enum.successful then
		
			send_campaign.options.AddSendedCampaign(send_campaign.campaign_file_crc)
		
			local dialog_sending_complete = sf.gui.CWindow()
			send_campaign.LoadWindow(_owner, dialog_sending_complete, _T("s4_sending_complete"))
			local dialog_sending_complete_ptr = sf.gui.CBaseWindowPtrT(dialog_sending_complete)
			
			local label_text = __cast(dialog_sending_complete.GetWidget(_T("label_text")), sf.gui.CLabelWidget)
			
			label_text.SetText(_T(string.format(T_(send_campaign.strings[3]).c_str(), T_(send_campaign.campaign_name).c_str())))
			
			_owner.DoModal(dialog_sending_complete_ptr)
		
		end
	end
end
