<?php
/**
 * Ajax Controller
 */
class Ajax extends Controller
{
    /**
     * Process
     */
    public function process()
    {
        $AuthUser   = $this->getVariable("AuthUser");
        $Route      = $this->getVariable("Route");
        $this->{$Route->params->ajax}();
    }

    public function embed() { 
        $Route      = $this->getVariable("Route"); 
        $AuthUser      = $this->getVariable("AuthUser"); 
        $Return = null;
        $client = new \GuzzleHttp\Client();
        if(isset($Route->params->action)) {
            $Listing = $this->db->from('posts')->where('id',$Route->params->action,'=')->first();
            $Data           = json_decode($Listing['data'], true);

            if($Listing['type'] == 'upload' AND $Listing['source']) {
                
                $sessionGame = 'game:'.$Listing['id'];
                if(empty($_SESSION[$sessionGame])) {
                    $_SESSION[$sessionGame] = $sessionGame;  
                    $this->db->update('posts')->where('id',$Listing['id'])->set(array('view' => (int)($Listing['view']+1)));
                    if(isset($AuthUser['id'])) {
                        $GameLog = $this->db->from('posts_log')->where('post_id',$Listing['id'],'=')->where('user_id',$AuthUser['id'])->first();
                        if(empty($GameLog['id'])) {
                            $this->db->insert('posts_log')->set(array(
                                'post_id' => $Listing['id'],
                                'user_id' => $AuthUser['id']
                            ));
                        }
                    }
                } 
                $response = $client->request('GET', $Listing['source']);
                echo $response->getBody();
            }elseif($Listing['type'] == 'remote' AND $Data['url']) {

                $sessionGame = 'game:'.$Listing['id'];
                if(empty($_SESSION[$sessionGame])) {
                    $_SESSION[$sessionGame] = $sessionGame;  
                    $this->db->update('posts')->where('id',$Listing['id'])->set(array('view' => (int)($Listing['view']+1)));
                    if(isset($AuthUser['id'])) {
                        $GameLog = $this->db->from('posts_log')->where('post_id',$Listing['id'],'=')->where('user_id',$AuthUser['id'])->first();
                        if(empty($GameLog['id'])) {
                            $this->db->insert('posts_log')->set(array(
                                'post_id' => $Listing['id'],
                                'user_id' => $AuthUser['id']
                            ));
                        }
                    }
                } 
                $response = $client->request('GET', $Data['url'], [
                    'allow_redirects' => [
                        'max'             => 10,        // allow at most 10 redirects.
                        'strict'          => true,      // use "strict" RFC compliant redirects.
                        'referer'         => true,      // add a Referer header
                        'protocols'       => ['https'], // only allow https URLs
                        'track_redirects' => true
                    ]
                ]);
                echo $response->getBody();
            }else{
                $Return = null;
            }
        }
    } 
    public function earnxp() {
        $AuthUser   = $this->getVariable("AuthUser");
        $Settings   = $this->getVariable("Settings");

        $sessionGame = 'game:'.Input::cleaner($_POST['game_id']);
        if(empty($_SESSION[$sessionGame]) AND isset($AuthUser['id'])) {
            $_SESSION[$sessionGame] = $sessionGame;  
            $this->db->update('users')->where('id',$AuthUser['id'])->set(array('xp' => $AuthUser['xp']+get($Settings,'data.play_xp','customize')));
        }
    }
    public function reaction() {  
        $AuthUser   = $this->getVariable("AuthUser");
        if($AuthUser['id']) {
            $Vote = $this->db->from('posts_like')->where('user_id',$AuthUser['id'])->where('post_id',$_POST['id'])->first();
            if(Input::cleaner($_POST['type']) == '-up' || Input::cleaner($_POST['type']) == '-down') {
                $this->db->delete('posts_like')->where('id',$Vote['id'],'=')->done(); 
            } elseif($Vote['id']) {
                $dataarray          = array(
                    "reaction"          => Input::cleaner($_POST['type'])
                );   
                $this->db->update('posts_like')->where('id',$Vote['id'])->set($dataarray);  
            } elseif(!$Vote['id']) {
                $dataarray          = array(
                    "user_id"       => $AuthUser['id'],
                    "post_id"       => Input::cleaner($_POST['id']),
                    "reaction"      => Input::cleaner($_POST['type'])
                );   
                $this->db->insert('posts_like')->set($dataarray);  
            }
        }
        return true; 
    }
}