<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class File extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function open($title = null, $download = false)
    {
        if ($title == null) {
            redirect(base_url());
        } else {
            $file = $this->db->query('SELECT name, type, path, isUserData FROM files WHERE name = ?', [urldecode($title)])->result_array();

            if (!empty($file)) {
                $file = $file[0];
//                 TODO: FIX!
//                header("Content-length: " . $file['size']);
                header("Content-type: " . $file['type']);
                if ($download) {
                    header("Content-Disposition: attachment; filename=" . $file['name'] . '.' . explode('/', $file['type'])[1]);
                }

                $isThumb = isset($_GET['w']) || isset($_GET['h']);
                $w = isset($_GET['w']) ? $_GET['w'] : false;
                $h = isset($_GET['h']) ? $_GET['h'] : false;
                $basePath = str_replace('\\', DIRECTORY_SEPARATOR, $file['path']);
				$basePath = str_replace('/', DIRECTORY_SEPARATOR, $basePath);
                $imagePath = pathinfo($basePath, PATHINFO_DIRNAME) . '/' .
                    ($isThumb ? 'thumbs/' : '') .
                    $file['name'] .
                    ($w ? '_w' . $w : '') .
                    ($h ? '_h' . $h : '') .
                    '.' . pathinfo($basePath, PATHINFO_EXTENSION);
				$imagePath = str_replace('/', DIRECTORY_SEPARATOR, $imagePath);

                if (!file_exists($imagePath)) {
                    if(!$isThumb)
                        exit;

                    $dir = pathinfo($imagePath, PATHINFO_DIRNAME);
                    if(!file_exists($dir)) {
                        mkdir(pathinfo($dir, 0777));
                    }

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $file['path'];
                    if (isset($_GET['w'])) {
                        $config['width'] = $_GET['w'];
                    }
                    if (isset($_GET['h'])) {
                        $config['height'] = $_GET['h'];
                    }
                    $config['maintain_ratio'] = TRUE;
                    $config['new_image'] = $imagePath;

                    $this->load->library('image_lib', $config);

                    if (!$this->image_lib->resize()) {
                        echo $this->image_lib->display_errors();
                    }
                }
                readfile(base_url($imagePath));
                exit;
            } else {
                redirect(base_url());
            }
        }
    }

    public function thumbnail($videoID = null)
    {
        if ($videoID !== null && strlen($videoID) == 11) {
            $thumbnail_url = "http://img.youtube.com/vi/" . $videoID . "/maxresdefault.jpg";
            header("Content-Type: image/jpeg");
            readfile($thumbnail_url);
        } else {
            redirect(base_url());
        }
    }

    public function csgo($category = null, $item = null)
    {
        if ($category == null || $item == null) {
            redirect(base_url());
        } else {
            if ($category == 'weapon') {
                header("Content-Type: image/png");
                readfile('http://csgo-stats.com/img/weapons/3d/' . $item . '.png');
            } elseif ($category == 'map') {
                header("Content-Type: image/jpeg");
                readfile('http://csgo-stats.com/img/maps/' . $item . '.jpg');
            } elseif ($category = 'mapicon') {
                header("Content-Type: image/png");
                readfile('http://csgo-stats.com/img/maps/icons/' . $item . '.png');
            }
        }
    }

    public function weapon()
    {
        header("Content-Type: image/jpeg");
        readfile('http://csgo-stats.com/img/weapons/bg.jpg');
    }
}