package sfs2x.extension.test.signup;

import java.util.Arrays;
import java.util.HashMap;

import com.smartfoxserver.v2.components.login.ILoginAssistantPlugin;
import com.smartfoxserver.v2.components.login.LoginAssistantComponent;
import com.smartfoxserver.v2.components.login.LoginData;
import com.smartfoxserver.v2.components.signup.ISignUpAssistantPlugin;
import com.smartfoxserver.v2.components.signup.PasswordMode;
import com.smartfoxserver.v2.components.signup.RecoveryMode;
import com.smartfoxserver.v2.components.signup.SignUpAssistantComponent;
import com.smartfoxserver.v2.components.signup.SignUpConfiguration;
import com.smartfoxserver.v2.components.signup.SignUpErrorCodes;
import com.smartfoxserver.v2.components.signup.SignUpValidationException;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;
import com.smartfoxserver.v2.security.DefaultPermissionProfile;

public class SignUpTestExtension extends SFSExtension
{
	private SignUpAssistantComponent suac;
	private LoginAssistantComponent lac; 
	
	private static final class CustomValidator implements ISignUpAssistantPlugin
	{
		private final String zoneName;
		
		public CustomValidator(String zoneName)
        {
			this.zoneName = zoneName;
        }
		
		@Override
		public void execute(User user, ISFSObject params, SignUpConfiguration config) throws SignUpValidationException
		{
		    Integer age = params.getInt("age");
		    String country = params.getUtfString("country");
		    
		    if (age == null || age < 5)
		    	throw new SignUpValidationException(SignUpErrorCodes.CUSTOM_ERROR, "Your age should be greater than 5 yrs. old!");
		    
		    if (country == null || country.length() < 2)
		    	throw new SignUpValidationException(SignUpErrorCodes.CUSTOM_ERROR, "Pleas specify your country");
		    
		    String message = "";
		    
		    if (age < 18) 
				message = "<p>Since your age is below 18, in-game purchases will not be available</p>";
			
		    HashMap<String, String> customMailFields = new HashMap<String, String>();
			customMailFields.put("${ageComment}", message);
			config.emailResponse.customEmailFields = customMailFields;	
		
		    // Add the Zone id
		    params.putUtfString("zone", zoneName);
		    
		}
	}

	@Override
	public void init()
	{
		initSignUpAssistant();
		initLoginAssistant();
	}
	
	@Override
	public void destroy()
	{
	    super.destroy();
	    lac.destroy();
	}
	
	private void initSignUpAssistant()
	{
		suac = new SignUpAssistantComponent();
		
		suac.getConfig().passwordMode = PasswordMode.PLAIN_TEXT;
		suac.getConfig().signUpTable = "signup_test";
		suac.getConfig().extraFields = Arrays.asList("country", "age", "zone");
		suac.getConfig().activationCodeField = "act_code";
		suac.getConfig().preProcessPlugin = new CustomValidator(getParentZone().getName());
		
		suac.getConfig().emailResponse.isActive = true;
		suac.getConfig().emailResponse.fromAddress = "someone@somewebsite.net";
		suac.getConfig().emailResponse.subject = "Thanks for signing up";
		suac.getConfig().emailResponse.template = "SignUpEmailTemplates/SignUpConfirmation.html";
		
		// Password recovery setup
		suac.getConfig().passwordRecovery.isActive = true;
		suac.getConfig().passwordRecovery.mode = RecoveryMode.SEND_OLD;
		suac.getConfig().passwordRecovery.email.fromAddress = "someone@somewebsite.net";
		suac.getConfig().passwordRecovery.email.subject = "Password recovery service";
		suac.getConfig().passwordRecovery.email.template = "SignUpEmailTemplates/PasswordRecovery.html";
		
		addRequestHandler(SignUpAssistantComponent.COMMAND_PREFIX, suac);
	}
	
	private void initLoginAssistant()
	{
		lac = new LoginAssistantComponent(this);
		
		lac.getConfig().loginTable = "signup_test";
		lac.getConfig().userNameField = "email";
		lac.getConfig().passwordField = "password";
		lac.getConfig().nickNameField = "username";
		lac.getConfig().activationField = "active";
		lac.getConfig().activationErrorMessage = "Your account has not been activated yet!";
		
		lac.getConfig().extraFields = Arrays.asList("country", "age");
		
		lac.getConfig().postProcessPlugin = new ILoginAssistantPlugin()
		{
			@Override
			public void execute(LoginData loginData)
			{
				loginData.session.setProperty("$permission", DefaultPermissionProfile.STANDARD);
			}
		};
	}
}
