<?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 upload() {   
            
        $ImageName = randomcode();
        $image      = uploader($_FILES['file'],$ImageName,UPLOADPATH.'/'.MEDIA_FOLDER.'/',MEDIA_X,'png');
        uploader($_FILES['file'],$ImageName,UPLOADPATH.'/'.MEDIA_FOLDER.'/',MEDIA_X,'webp');
        uploader($_FILES['file'],'thumb-'.$ImageName,UPLOADPATH.'/'.MEDIA_FOLDER.'/',MEDIA_THUMB_X.','.MEDIA_THUMB_Y,'png');
        uploader($_FILES['file'],'thumb-'.$ImageName,UPLOADPATH.'/'.MEDIA_FOLDER.'/',MEDIA_THUMB_X.','.MEDIA_THUMB_Y,'webp');
            
        echo json_encode(array(
                'file_name' => $image,
                'file_path' => UPLOADPATH.'/'.MEDIA_FOLDER.'/'.$image
        ));
    }

    public function posts() {
        $Route      = $this->getVariable("Route");  
        if($_GET['q']) {
            $Listings = $this->db->from(null,'
                SELECT *
                FROM `posts`   
                WHERE title LIKE "%'.$_GET['q'].'%"
                ORDER BY id DESC
                LIMIT 0,4')
                ->all();
            $posts = null;
            foreach ($Listings as $Listing) {
                $posts[] = [
                    'id'            => $Listing['id'],
                    'title'         => $Listing['title'], 
                    'type'          => $Listing['type'], 
                    'image'         => UPLOAD.'/'.GAME_FOLDER.'/'.$Listing['image']
                ];  
            } 
        }
        echo json_encode(array(
            "data"   => $posts
        ));
    }
    public function addgame() {
        $Listing        = json_decode($_POST['data'], true);
    
        if(isset($Listing['title']))
            $ImageName      = randomcode();
            $SourceName     = randomcode();

            if(!empty($Listing['image'])) {
                if (!file_exists(UPLOADPATH . '/temp/')) {
                    mkdir(UPLOADPATH . '/temp/', 0777, true);
                }
                $path_image = UPLOADPATH . '/temp/' . randomcode() . '.png';
                downloader($Listing['image'], $path_image);
                $_FILES['image'] = $path_image;
            }
            if(isset($_FILES['image']) AND $_FILES['image']['name']) {
                $image      = uploader($_FILES['image'],$ImageName,UPLOADPATH.'/'.GAME_FOLDER.'/',GAME_X.','.GAME_Y,'png');
                uploader($_FILES['image'],$ImageName,UPLOADPATH.'/'.GAME_FOLDER.'/',GAME_X.','.GAME_Y,'webp');
                uploader($_FILES['image'],'thumb-'.$ImageName,UPLOADPATH.'/'.GAME_FOLDER.'/',GAME_THUMB_X.','.GAME_THUMB_Y,'png');
                uploader($_FILES['image'],'thumb-'.$ImageName,UPLOADPATH.'/'.GAME_FOLDER.'/',GAME_THUMB_X.','.GAME_THUMB_Y,'webp');
                unlink($path_image);
            }
 

            $dataarray          = array(
                "type"              => 'remote',
                "title"             => Input::cleaner($Listing['title']),
                "self"              => Input::seo($Listing['title']),
                "image"             => isset($image) ? $image : null,
                "description"       => Input::cleaner($Listing['description']),
                "control"           => Input::cleaner($Listing['control']),
                "source"            => isset($Listing['file']) ? $Listing['file'] : null,
                "keywords"          => Input::cleaner($Listing['tags']), 
                "resolution"        => (isset($Listing['width']) ? $Listing['width'].'x'.$Listing['height'] : IFRAME_X.'x'.IFRAME_Y),
                "view"              => 0,
                "status"            => 1,
                "api_id"            => Input::cleaner($Listing['id']), 
                "api_type"          => Input::cleaner($Listing['api_type']), 
                "created"           => date('Y-m-d H:i:s')
            );   
            $this->db->insert('posts')->set($dataarray); 

            $post_id     = $this->db->lastId(); 

            // Genres
            if (isset($_POST['category'])) { 
                $dataarray = array(
                    "category_id"   => $_POST['category'],
                    "post_id"       => $post_id
                );
                $this->db->insert('posts_category')->set($dataarray);
            }
            removefolder(PATH.'/cache');
    }
    public function post() {
        $Route      = $this->getVariable("Route"); 
        if($_GET['id']) {
            $Listing = $this->db->from(null,'
                SELECT *
                FROM `posts`   
                WHERE posts.id = '.$_GET['id'])
                ->first();  

            $result = [
                'id'                => $Listing['id'],   
                'type'              => $Listing['type'], 
                'title'             => $Listing['title'], 
                'image'             => UPLOAD.'/'.GAME_FOLDER.'/'.$Listing['image']
            ];  
          
        }
        echo json_encode(array( 
            "data"      => $result
        ));
    }
}