/*
 *  This file is part of SWADroid.
 *
 *  Copyright (C) 2010 Juan Miguel Boyero Corral <juanmi1982@gmail.com>
 *
 *  SWADroid is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SWADroid is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SWADroid.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.ugr.swad.swadroid.modules;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import es.ugr.swad.swadroid.Base64;
import es.ugr.swad.swadroid.Global;
import es.ugr.swad.swadroid.R;
import es.ugr.swad.swadroid.model.User;
import es.ugr.swad.swadroid.modules.rollcall.Util;

/**
 * Login module for connect to SWAD.
 * @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
 * @author Antonio Aguilera Malagon <aguilerin@gmail.com>
 */
public class Login extends Module {
	/**
	 * Logged user
	 */
	private User loggedUser;
	/**
	 * Digest for user password.
	 */
	private MessageDigest md;
	/**
	 * User ID.
	 */
	private String userID;
	/**
	 * User password.
	 */
	private String userPassword;
	/**
	 * Login tag name for Logcat
	 */
	public static final String TAG = Global.APP_TAG + " Login";

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate()
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setMETHOD_NAME("loginByUserPasswordKey");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();      
		connect();
	}

	/**
	 * Launches action in a separate thread while shows a progress dialog
	 * in UI thread.
	 */
	protected void connect() {
		String progressDescription = getString(R.string.loginProgressDescription);
		int progressTitle = R.string.loginProgressTitle;
		Connect con = new Connect(false, progressDescription, progressTitle, true);

		/*if(!Global.isLogged())
    		Toast.makeText(this, progressDescription, Toast.LENGTH_LONG).show();*/

		con.execute();
	} 
	
	public static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException nfe) {}
        return false;
    }

	/**
	 * Connects to SWAD and gets user data.
	 * @throws NoSuchAlgorithmException
	 * @throws IOException
	 * @throws XmlPullParserException
	 * @throws SoapFault
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 */
	protected void requestService()
			throws NoSuchAlgorithmException, IOException, XmlPullParserException, SoapFault, IllegalAccessException, InstantiationException {

		//If last login time > Global.RELOGIN_TIME, force login
		if(System.currentTimeMillis()-Global.getLastLoginTime() > Global.RELOGIN_TIME) {
			Global.setLogged(false);
		}

		//If the application isn't logged, force login
		if(!Global.isLogged())
		{
			//Remove left and right spaces
			userID = prefs.getUserID().trim();
			
			//If the user ID is a DNI
			if(Util.isValidDni(userID)) {
				//If the DNI has no letter, remove left zeros
				if(isInteger(userID)) {
					userID = String.valueOf(Integer.parseInt(userID));
					
				//If the last position of the DNI is a char, remove it
				} else if(isInteger(userID.substring(0, userID.length()-1))) {
					userID = String.valueOf(Integer.parseInt(userID.substring(0, userID.length()-1)));					
				}
			}
			
			//Encrypts user password with SHA-512 and encodes it to Base64UrlSafe   	
			md = MessageDigest.getInstance("SHA-512");
			md.update(prefs.getUserPassword().getBytes());
			userPassword = new String(Base64.encodeBytes(md.digest()));
			userPassword = userPassword.replace('+','-').replace('/','_').replace('=', ' ').replaceAll("\\s+", "").trim();

			//Creates webservice request, adds required params and sends request to webservice
			createRequest();
			addParam("userID", userID);
			addParam("userPassword", userPassword);
			addParam("appKey", Global.getAppKey());
			sendRequest(User.class, true);

			if (result != null) {
				KvmSerializable ks = (KvmSerializable) result;
				SoapObject soap = (SoapObject)result;
				
				/*Log.i(TAG, "count=" + ks.getPropertyCount());
				Log.i(TAG, "property[0]=" + ks.getProperty(0));
				Log.i(TAG, "property[1]=" + ks.getProperty(1));
				Log.i(TAG, "property[2]=" + ks.getProperty(2));
				Log.i(TAG, "property[3]=" + ks.getProperty(3));
				Log.i(TAG, "property[4]=" + ks.getProperty(4));
				Log.i(TAG, "property[5]=" + ks.getProperty(5));
				Log.i(TAG, "property[6]=" + ks.getProperty(6));
				Log.i(TAG, "property[7]=" + ks.getProperty(7));
				Log.i(TAG, "property[8]=" + ks.getProperty(8));
				 */

				//Stores user data returned by webservice response
				loggedUser = new User(
						Long.parseLong(ks.getProperty(0).toString()),					// id
						soap.getProperty("wsKey").toString(),							// wsKey
						soap.getProperty("userID").toString(),							// userID
						//soap.getProperty("userNickname").toString(),					// userNickname
						null,															// userNickname
						soap.getProperty("userSurname1").toString(),					// userSurname1
						soap.getProperty("userSurname2").toString(),					// userSurname2
						soap.getProperty("userFirstname").toString(),					// userFirstname
						soap.getProperty("userPhoto").toString(),						// photoPath
						Integer.parseInt(soap.getProperty("userRole").toString())		// userRole
						);

				Global.setLoggedUser(loggedUser);

				//Update application last login time
				Global.setLastLoginTime(System.currentTimeMillis());
			}
		}

		/*if(isDebuggable) {
			Log.d(TAG, "id=" + loggedUser.getId());
			Log.d(TAG, "wsKey=" + loggedUser.getWsKey());
			Log.d(TAG, "userID=" + loggedUser.getUserID());
			Log.d(TAG, "userNickname=" + loggedUser.getUserNickname());
			Log.d(TAG, "userSurname1=" + loggedUser.getUserSurname1());
			Log.d(TAG, "userSurname2=" + loggedUser.getUserSurname2());
			Log.d(TAG, "userFirstName=" + loggedUser.getUserFirstname());
			Log.d(TAG, "userRole=" + loggedUser.getUserRole());
			Log.d(TAG, "lastLoginTime=" + Global.getLastLoginTime());
		}*/

		//Request finalized without errors
		setResult(RESULT_OK);
	}

	/* (non-Javadoc)
	 * @see es.ugr.swad.swadroid.modules.Module#postConnect()
	 */
	@Override
	protected void postConnect() {
		finish();
	}

	/* (non-Javadoc)
	 * @see es.ugr.swad.swadroid.modules.Module#onError()
	 */
	@Override
	protected void onError() {
	}
}
