<?php

class Torrents_model extends CI_Model
{

    public function __construct()
    {
        $this->data_listing_context = 'torrents.id , torrents.comments_count , torrents.publish_date , torrents.name ,';
        $this->data_listing_context .= 'torrents.size , torrents.seed , torrents.leech , torrents.category ,';
        $this->data_listing_context .= 'categoriesp.name AS parent_category_name , categoriesc.name AS sub_category_name';
    }

    public function get_parent_categories($order = 'order')
    {
        return $this->db->where('parent', 0)->order_by($order, 'asc')->get('categories')->result();
    }

    public function get_parent_categories_noadult($order = 'order')
    {
        return $this->db->where('parent', 0)->order_by($order, 'asc')->where('id !=', 2188)->get('categories')->result();
    }
    
    public function get_categorie_data($id)
    {
        return $this->db->where('id', $id)->get('categories')->row();
    }

    public function get_sub_categorie_data($parent_id, $order = 'name')
    {
        return $this->db->where('parent', $parent_id)->order_by($order, 'asc')->get('categories')->result();
    }

    public function get_parent_categorie_data($id)
    {
        return $this->db->select('c2.*')->from('categories as c1')->where('c1.id', $id)->join('categories as c2', 'c1.parent = c2.id')->get()->row();
    }

    public function get_parent_categoriesid()
    {
        return $this->db->select('id , name')->from('categories')->where('parent', 0)->order_by('order', 'asc')->get()->result();
    }

    public function get_parent_sub_categoriesid()
    {
        return $this->db->select('id , name , parent')->from('categories')->where('parent !=', 0)->order_by('order', 'asc')->get()->result();
    }

   public function get_exclu_torrents($limit,$offset) {
        return $this->db->select('torrents.id, torrents.state, torrents.age, torrents.comments_count, torrents.publish_date, torrents.name, torrents.category_slug, torrents.size, torrents.seed, torrents.leech, torrents.completed, categories.id AS category_id, categories.name AS category')
                ->from('torrents')
                ->where('is_exclusivity', 1)
                ->join('categories','torrents.category = categories.id')
                ->order_by('id','desc')
                ->limit($limit,$offset)
                ->get()
                ->result();
    }


    private function get_prototype_listing($category, $sort, $order, $limit, $offset)
    {
        $this->db->select($this->data_listing_context)
            ->from('torrents')
            ->where('torrents.category', (int) $category)
            ->where('torrents.state', 0)
            ->join('categories AS categoriesp', 'torrents.parent_category = categoriesp.id')
            ->join('categories AS categoriesc', 'torrents.category = categoriesc.id')
            ->order_by($sort, $order)
            ->limit($limit, $offset);

        return $this->db;
    }

    public function get_period_torrents($type, $category, $sort = 'publish_date', $order = 'desc', $limit = 30, $offset = 0)
    {
        $query = $this->get_prototype_listing($category, $sort, $order, $limit, $offset);

        if ($type == 'today_torrents') {
            $where = 'torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)';
        } elseif ($type == 'week_torrents') {
            $where = 'torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)';
        } else {
            $where = 'torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 30 DAY)';
        }

        return $query->where($where)->get()->result();
    }

    public function updateCategoriesStats($parent_cat_id, $sub_cat_id)
    {
        // mis à jour nombre torrents dans la catégorie parente
        $this->db->set('torrents', 'torrents + 1', false);
        $this->db->where('id', $parent_cat_id)->update('categories');
        // mis à jour nombre torrents dans la catégorie fille
        $this->db->set('torrents', 'torrents + 1', false);
        $this->db->where('id', $sub_cat_id)->update('categories');
        return true;
    }

    public function generate_category_slug($id)
    {
        $this->db->select('c1.name AS sub_category_name , c1.id AS sub_category_id , c2.name AS parent_category_name , c2.id AS parent_category_id');
        $this->db->from('categories AS c1');
        $this->db->where('c1.id', $id);
        $this->db->join('categories AS c2', 'c1.parent = c2.id');
        $result = $this->db->get()->row();
        // mis à jour statististiques
        $this->updateCategoriesStats($result->sub_category_id, $result->parent_category_id);
        return url_title($result->parent_category_name, '-', true) . '/' . url_title($result->sub_category_name, '-', true);
    }

    public function torrents_popular($categorie, $limit = 2)
    {
        return $this->db->select('torrents.name , torrents.id , torrents.seed , torrents.leech , torrents.age , torrents.size , torrents.category_slug')
            ->from('track_views as t1')
            ->where('t1.date > UNIX_TIMESTAMP(NOW() - INTERVAL 3 DAY)')
            ->group_by('t1.guid', 'desc')
            ->order_by('count(guid)', 'desc')
            ->where('torrents.parent_category', $categorie)
            ->where('torrents.state', 0)
            ->join('torrents', 't1.guid = torrents.id')
            ->limit($limit)
            ->get()
            ->result();

    }

    public function last_torrents($categorie)
    {
        return $this->db->select('torrents.name , torrents.id , torrents.seed , torrents.leech , torrents.age , torrents.size , torrents.category_slug')
            ->from('torrents')
            ->order_by('torrents.id', 'desc')
            ->where('torrents.parent_category', $categorie)
            ->where('torrents.state', 0)
            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
            ->limit(30)
            ->get()
            ->result();
    }

    public function week_torrents($categorie)
    {
        return $this->db->select('torrents.name , torrents.id , torrents.seed , torrents.leech , torrents.age , torrents.size , torrents.category_slug')
            ->from('torrents')
            ->order_by('torrents.id', 'desc')
            ->where('torrents.parent_category', $categorie)
            ->where('torrents.state', 0)
            ->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)')
            ->limit(30)
            ->get()
            ->result();
    }

    public function getMostSeeded()
    {
        return $this->db->select('torrents.id, torrents.name, torrents.seed, torrents.leech, torrents.completed, torrents.age, torrents.size, torrents.category_slug, torrents.parent_category, torrents.category, torrents.publish_date, torrents.comments_count , users.nickname AS uploader')->from('torrents')->join('users', 'torrents.uploader = users.id')->where('torrents.seed >', '0')->order_by('torrents.seed', 'desc')->where('torrents.state', 0)->limit(100)->get()->result_array();
    }

    public function getMostCompleted()
    {
        return $this->db->select('torrents.id, torrents.name, torrents.seed, torrents.leech, torrents.completed, torrents.age, torrents.size, torrents.category_slug, torrents.parent_category, torrents.category, torrents.publish_date, torrents.comments_count , users.nickname AS uploader')->from('torrents')->where('torrents.parent_category !=','2188')->join('users', 'torrents.uploader = users.id')->where('torrents.seed >', '0')->where('torrents.comments_count >', '20')->order_by('torrents.completed', 'desc')->where('torrents.state', 0)->limit(100)->get()->result_array();
    }

    public function get_torrent_from_period($type, $out = '')
    {
        if ($type == 'day') {
            $this->db->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)');
            //$this->db->where('torrents.seed >', '1');
            //$this->db->where('torrents.completed >', '5');
        } else if ($type == 'week') {
            $this->db->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)');
            $this->db->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 8 DAY)');
            $this->db->where('torrents.seed >', 20);
            //$this->db->where('torrents.completed >', 30);
        } else if ($type == 'month') {
            $this->db->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 8 DAY)');
            $this->db->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 38 DAY)');
            $this->db->where('torrents.seed >', 25);
            //$this->db->where('torrents.completed >', 150);
        }
        $this->db->select('torrents.id, torrents.name, torrents.seed, torrents.leech, torrents.completed, torrents.age, torrents.size, torrents.category_slug, torrents.parent_category, torrents.category, torrents.publish_date, torrents.comments_count , users.nickname AS uploader')->from('torrents')->join('users', 'torrents.uploader = users.id')->order_by('torrents.id', 'desc')->where('torrents.state', 0);
        return $out == 'object' ? $this->db->get()->result() : $this->db->get()->result_array();
    }

    public function get_torrent($id)
    {
        return $this->db->select('torrents.* , users.nickname AS nickname, users.settings AS user_settings')->from('torrents')->join('users', 'torrents.uploader = users.id')->where('torrents.id', $id)->get()->row();
    }

    public function get_torrent_comments($guid, $limit = 15, $offset = 0)
    {
        return $this->db->select('comments.* , users.nickname AS publisher , users.last_activity_date AS publisher_activity , users.uploaded AS up , users.downloaded AS down , users.id AS publisher_id  , users.rank AS publisher_rank , users.avatar AS avatar')
            ->from('comments')
            ->where('comments.guid', $guid)
            ->order_by('comments.id', 'desc')
            ->join('users', 'comments.publisher = users.id')
            ->limit($limit, $offset)
            ->get()->result();
    }

    public function get_focused_comment($id, $torrent)
    {
        return $this->db->select('comments.* , users.nickname AS publisher , users.last_activity_date AS publisher_activity , users.uploaded AS up , users.downloaded AS down , users.id AS publisher_id  , users.rank AS publisher_rank , users.avatar AS avatar')
            ->from('comments')
            ->where('comments.id', $id)
            ->where('comments.guid', $torrent)
            ->order_by('comments.id', 'desc')
            ->join('users', 'comments.publisher = users.id')
            ->get()->row();
    }

    public function get_from_ids($torrents_id)
    {
        $sql = 'SELECT `torrents`.*, `users`.`nickname` AS `nickname`, `categories`.`id` AS `category_id`, `categories`.`name` AS `category`';
        $sql .= 'FROM `torrents` JOIN `users` ON `torrents`.`uploader` = `users`.`id` JOIN `categories` ON `torrents`.`category` = `categories`.`id`';
        $sql .= "WHERE `torrents`.`id` IN ('$torrents_id') AND `torrents`.`state` = 0 ORDER BY FIELD (torrents.id,'$torrents_id') DESC";
        return $this->db->query($sql)->result();
    }

    public function get_today_torrents($category, $sort = 'publish_date', $order = 'desc', $limit = 30, $offset = 0)
    {
        return $this->db->select($this->torrent_data)
            ->from('torrents')
            ->join('users', 'torrents.uploader = users.id')
            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
            ->where('torrents.parent_category', $category)
            ->where('torrents.state', 0)
            ->join('categories', 'torrents.category = categories.id')
            ->order_by($sort, $order)
            ->limit($limit, $offset)
            ->get()
            ->result();
    }

    public function count_today_torrents($category)
    {
        return $this->db->select('torrents.id')
            ->from('torrents')
            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
            ->where('torrents.parent_category', $category)
            ->join('categories', 'torrents.category = categories.id')
            ->get()
            ->num_rows();
    }

    public function get_yesterday_torrents($category, $sort = 'publish_date', $order = 'desc', $limit = 30, $offset = 0)
    {
        return $this->db->select($this->torrent_data)
            ->from('torrents')
            ->join('users', 'torrents.uploader = users.id')
            ->where('torrents.publish_date < UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')
            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 2 DAY)')
            ->where('torrents.state', 0)
            ->where('torrents.parent_category', $category)
            ->join('categories', 'torrents.category = categories.id')
            ->order_by($sort, $order)
            ->get()
            ->result();
    }

    public function count_yesterday_torrents($category)
    {
        return $this->db->select('torrents.id')
            ->from('torrents')
            ->where('torrents.publish_date > UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)')
            ->where('torrents.parent_category', $category)
            ->join('categories', 'torrents.category = categories.id')
            ->get()
            ->num_rows();
    }

    public function get_popular_torrents($category, $sort = 'seed', $order = 'desc', $limit = 12, $offset = 0)
    {
        return $this->db->select($this->torrent_data)
            ->from('torrents')
            ->join('users', 'torrents.uploader = users.id')
            ->where('torrents.parent_category', (int) $category)
            ->where('torrents.state', 0)
            ->join('categories', 'torrents.category = categories.id')
            ->order_by($sort, $order)
            ->limit($limit, $offset)
            ->get()
            ->result();
    }

    public function count_popular_torrents($category)
    {
        return $this->db->select('torrents.id')
            ->from('torrents')
            ->where('torrents.parent_category', (int) $category)
            ->join('categories', 'torrents.category = categories.id')
            ->get()
            ->num_rows();
    }

    public function get_category_icon($name)
    {
        return $this->db->where('name', $name)->get('categories')->row()->icon;
    }

    public function get_torrents_all_categories($sort_type)
    {

        //$this->output->enable_profiler();
        $categories = $this->get_categories();
        $torrents   = array();
        if ($sort_type == 'popular') {
            foreach ($categories as $category) {
                $torrents[$category->name]                       = $this->get_popular_torrents($category->id);
                $torrents[$category->name]['parent_category_id'] = $category->id;
            }
        }
        if ($sort_type == 'today') {
            foreach ($categories as $category) {
                $torrents[$category->name]                       = $this->get_today_torrents($category->id);
                $torrents[$category->name]['parent_category_id'] = $category->id;
            }
        }
        if ($sort_type == 'yesterday') {
            foreach ($categories as $category) {
                $torrents[$category->name]                       = $this->get_yesterday_torrents($category->id);
                $torrents[$category->name]['parent_category_id'] = $category->id;
            }
        }
        return $torrents;
    }

    public function get_categories()
    {
        return $this->db->where('parent', '0')->order_by('order', 'asc')->get('categories')->result();
    }
}
