<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Login extends Controller {

	private $facebook;
	private $current_user;
	private $user_id;
	private $fb_user_id;
	private $secret;	
	private $app_id;	
	private $api_key;
	private $canvas_page;
	
	// model(s) variable(s)
	private $user;
	
	/**
	* run first
	*/
	public function before() {
		
		$this->current_user = NULL;
		$this->secret = '8e94e1c1b326151d72c492f10bf461c4';	
		$this->app_id = '103102906444847';	
		$this->api_key = '9155c0443f04a56a0bdcfbe9cadc255c';	
		$this->canvas_page = 'http://apps.facebook.com/deathmatchbest/';
	
		// load model variables
		$this->user = new Model_User();

		// include the facebook php SDK
		include Kohana::find_file('vendorf/Facebook', 'facebook');
		//require_once './application/vendor/facebook/src/facebook.php'; //dogru path
		$this->facebook = new Facebook(array(
			'appId'  => $this->app_id,
			'secret' => $this->secret,
			'cookie' => true,
		));		
		
		$session = $this->facebook->getSession();

		if ($session) { // if session is present
			try {
				$me = $this->facebook->api('/me'); // current logged in facebook user
				
				// check if the user is registered in the database, if not add user
				$users = $this->user->get(array('fb_user_id' => $me['id']));
				
				if(empty($users)) { // if user not in DB
					// enter user
					$add_param = array(
						'fb_user_id' => $me['id'],
						'first_name' => $me['first_name'], 
						'last_name' => $me['last_name'], 
						'avatar' => 'https://graph.facebook.com/' . $me['id'] . '/picture', 
						'profile_url' => $me['link']
					);
					//throw new exception( $this->fb_user_id);
					$user_id = $this->user->add($add_param);			
					
					$users = $this->user->get(array('id' => $user_id));
					//throw new exception("sdgdsfgd");
				} // end of if user not in DB
				Session::instance()->set("fbUserId",$me['id'] );
                                
                                //throw new exception(Session::instance()->get("fbUserId"));
				$this->current_user = $users[0];
				//Request::instance()->redirect("main/act");
			} catch (FacebookApiException $e) {
				//error_log($e);
			} // end of try getting FB user info
		} // end of if session is present
		
	} // end of before




    public function action_index($param=NULL) {
    		
		// authinticate facebook user here
		$user = new Model_User();
		$data = array(
			'app_id' => $this->app_id,
			'ajax_url' => url::site('fbkohana/ajax', 'http'),
			'current_user' => (!empty( $this->current_user ) ? $this->current_user : NULL),
			'canvas_page' => $this->canvas_page,
			'authorize_url' => 'https://www.facebook.com/dialog/oauth?client_id=' . $this->app_id . '&redirect_uri=' . $this->canvas_page,			
		);   
		//throw new exception(empty( $this->current_user ));
		//echo "<pre>"; print_r($data); echo "</pre>";
		
		//$view_location = (empty($param) ? 'facebook/index' : 'facebook/tab');
		//$me = $this->facebook->api('/me');
		//throw new exception( $this->fb_user_id);
		//$userx = ORM::factory("user")->where('fb_user_id','=',$me['id'])
                  //                      ->find();
		if($this->current_user){
			Request::instance()->redirect("main/act");
			//throw new exception("aaaa");
		}
		else	$this->request->response = View::factory('mainview',$data);		
    }
    /*
    public function action_index()
    {



  	if(Oauth::instance()->get_user()  //|| 
           //    Kohana_Facebook::instance()->get_user
        ){
    	    
    	    Request::instance()->redirect("main/act");
	}

	else	$this->request->response = View::factory('mainview');

    }

*/

}