package com.bestfare.pack;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.widget.Toast;

import java.util.HashMap;

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;
    private static String TAG = SessionManager.class.getSimpleName();
    private static final String KEY_IS_LOGGEDIN = "isLoggedIn";

    // Editor for Shared preferences
    Editor editor;
     boolean check;
    // Context
    Context _context;
     
    // Shared pref mode
    int PRIVATE_MODE = 0;
     
    // Sharedpref file name
    private static final String PREF_NAME = "BestFare";
    private static final String KEY_COUNTRY="country";
    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";
    private static final String KEY_PASSWORD = "Password";
    private  static final String isFirstRun="isFirstRun";
     
    // User name (make variable public to access from outside)
    public static String KEY_NAME = "name";
     
    // Email address (make variable public to access from outside)
    public static final String KEY_EMAIL = "email";
     
    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

/*

*
     * Create login session
     *
*/

    public void createLoginSession(String password,String email,String key_name){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);
         check=true;
                 // Storing name in pref
     editor.putString(KEY_PASSWORD, password);
         
        // Storing email in pref
        editor.putString(KEY_EMAIL, email);
        editor.putString(KEY_NAME, key_name);
         
        // commit changes
        editor.commit();
    }


/*
*
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     *
*/

    public void checkFirstLogin(){
        // Check login status
        Toast.makeText(_context, "in chck login", Toast.LENGTH_LONG).show();
        if(!this.isLoggedIn()){

            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context,LoginActivity.class );
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // Staring Login Activity
            _context.startActivity(i);

        }

    }
    public void setLogin(boolean isLoggedIn) {

        editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn);

        // commit changes
        editor.commit();

        Log.d(TAG, "User login session modified!");
    }
     
   /*
*
     * Get stored session data
     * */

    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();
        // user name
        user.put(KEY_NAME, pref.getString(KEY_NAME, null));
         
        // user email id
        user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
         
        // return user
        return user;
    }
/*
*
     * Clear session details
     * */

    public void logoutUser(){
        // Clearing all data from Shared Preferences
        _context.getSharedPreferences(PREF_NAME, 0).edit().clear().commit();
        editor.clear();
        editor.commit();
         
        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, LoginActivity.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         
        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         
        // Staring Login Activity
        _context.startActivity(i);
    }
/*
*
     * Quick check for login
     * **/

    // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(KEY_IS_LOGGEDIN,false);
    }
}
