<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Core_Request extends Francis_Core_Request {

    // Const
    const PARAM_AUTH = 'auth';

    /** @var TB_Server_Core_RequestData|null $data */
    public $data = NULL;
    public $auth = NULL;

    /**
     * @param $controller
     * @param $action
     * @param array $data
     */
    public function __construct( $controller, $action, array $data = array() ) {
        $this->controller = $controller;
        $this->action = $action;
        $this->data = new TB_Server_Core_RequestData( $data );

        // Simple auth structure
        $this->auth = array(
            'token' => NULL,
            'account_id' => NULL,
            'user' => NULL,
            'pass' => NULL,
            'hashed' => true
        );
    }

    /**
     * Set auth token
     *
     * @param string $token
     * @param integer $accountId
     */
    public function setAuthToken( $token, $accountId ) {
        $this->auth[ 'token' ] = '' . $token;
        $this->auth[ 'account_id' ] = (int)$accountId;
    }

    /**
     * Sets credentials
     * @param string $user
     * @param string $pass
     * @param bool $hashed
     */
    public function setAuthCredentials( $user, $pass, $hashed = true ) {
        $this->auth[ 'user' ] = '' . $user;
        $this->auth[ 'pass' ] = '' . $pass;
        $this->auth[ 'hashed' ] = $hashed;
    }

    /**
     * Create a request from given http request data
     *
     * @param array $httpRequest $_REQUEST data
     * @return Francis_Core_Request
     */
    public static function create( array $httpRequest = array() ) {
        // Revolve controller & action
        $controller = isset( $httpRequest[ self::PARAM_CONTROLLER ] ) ? $httpRequest[ self::PARAM_CONTROLLER ] : 'Index';
        $action = isset( $httpRequest[ self::PARAM_ACTION ] ) ? $httpRequest[ self::PARAM_ACTION ] : 'index';

        // Remove controller & action data from data
        unset( $httpRequest[ self::PARAM_CONTROLLER ] );
        unset( $httpRequest[ self::PARAM_ACTION  ] );

        $r = new TB_Server_Core_Request( $controller, $action, $httpRequest );

        // Resolve auth data
        $auth = isset( $httpRequest[ self::PARAM_AUTH ] ) ? $httpRequest[ self::PARAM_AUTH ] : NULL;
        if ( is_array( $auth ) ) {
            $r->auth[ 'token' ] = isset( $auth[ 'token' ] ) ? $auth[ 'token' ] : NULL;
            $r->auth[ 'account_id' ] = isset( $auth[ 'account_id' ] ) ? $auth[ 'account_id' ] : NULL;
            $r->auth[ 'user' ] = isset( $auth[ 'user' ] ) ? $auth[ 'user' ] : NULL;
            $r->auth[ 'pass' ] = isset( $auth[ 'pass' ] ) ? $auth[ 'pass' ] : NULL;
            $r->auth[ 'hashed' ] = isset( $auth[ 'hashed' ] ) ? $auth[ 'hashed' ] : NULL;
        }

        return $r;
    }

    /**
     * Creates a valid request url
     *
     * @param string $controller Name of controller class
     * @param string $action Action(=method) name of controller class
     * @param null|array $args (optional) is NULL
     * @param string $baseUrl Base URL Default is config url.client
     *
     * @return string
     */
    public static function createClientUrl( $controller = 'Index', $action = 'index', $args = NULL, $baseUrl = NULL ) {
        $baseUrl = is_string( $baseUrl ) ? $baseUrl : Francis_Utils_Config::get( 'url.client' );
        $url  = rtrim( $baseUrl, '/' ) . '/#/' . self::PARAM_CONTROLLER . '=';
        $url .= $controller;
        $url .= '&' . self::PARAM_ACTION . '=' . $action;

        if ( is_array( $args ) &&
            0 < count( $args ) ) {
            $url .= '&' . http_build_query( $args );
        }

        return $url;
    }
} 