<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Webtoon extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('post_model');
    }


    public function index()
    {
        if ($this->isMobile()) {
            $this->mobile();
        } else {
            $this->desktop();
        }
    }

    public function desktop()
    {
        $episodeURL = $this->uri->segment(2);
        $seriesURL = $this->uri->segment(3);
        if (is_numeric($episodeURL)) {
            $this->data['page'] = $this->post_model->getPost($episodeURL);
            if ($this->data['page']->postID != "") {
                $this->data['page']->postContentHTML = $this->getJsonToPostContent($this->data['page']->postContent);
                $this->data['hotToons'] = $this->getPostsRankingByRankTypeAndCategoryId('V', getChildrenByParentId(20000), 5);
                getResultWithAuthor($this->data['hotToons']);
                $this->data['episodes'] = $this->post_model
                    ->getPagePostsByPageId($this->data['page']->pageID, 100)
                    ->order_by('p.postID', 'asc')->get()->result();
                $this->load->view('templates/webtoon/webtoon-article', $this->data);
            } else {
                $this->error();
            }
        } elseif ($seriesURL) {
            $this->load->model('page_model');
            $this->data['seriesURL'] = $seriesURL;
            $this->data['series'] = $this->post_model->getPostsRatingByPageId($seriesURL, 100)->order_by('dateUpdated', 'desc')->get()->result();
            $this->data['page'] = $this->page_model->getOne($seriesURL);
            if (count($this->data['series'])) {
                $this->list();
                $this->load->view('templates/webtoon/webtoon-list', $this->data);
            } else {
                $this->error();
            }
        } elseif ($this->uri->segment(2) == 'series') {
            $this->series();
            $this->load->view('templates/webtoon/webtoon-series', $this->data);
        } else {
            $this->main();
            foreach ($this->data as $k => $v) {
                getPostsImageUrl($this->data[$k]);
            }
            $this->load->view('templates/webtoon/webtoon-home', $this->data);
        }
    }

    private function getPostsRankingByRankTypeAndCategoryId($rankType, $categoryId, $limit)
    {
        return $this->post_model->getPostsRankingByRankTypeAndCategoryId($rankType, $categoryId, $limit)
            ->join('cm_page_content co', 'pp.pageID=co.pageID', 'left')->get()->result();
    }

    private function getPostsByCategoryId($categories, $limit, $offset = 0)
    {
        return $this->post_model->getPostsByCategoryId($categories, $limit, $offset)
            ->join('cm_page_content co', 'pp.pageID=co.pageID', 'left')->get()->result();
    }

    public function main()
    {
        $this->data['featureToons'] = $this->getPostsRankingByRankTypeAndCategoryId('R', 20008, 8);
        getResultWithAuthor($this->data['featureToons']);

        foreach (['hotToons' => [20009, 8], 'funToons' => [20010, 3], 'touchedToons' => [20011, 3]] as $categoryName => list($categoryID, $rowCount)) {
            $this->data[$categoryName] = [
                'updates' => $this->getPostsByCategoryId($categoryID, $rowCount),
                'reads' => $this->getPostsRankingByRankTypeAndCategoryId('V', $categoryID, $rowCount),
                'recomm' => $this->getPostsRankingByRankTypeAndCategoryId('R', $categoryID, $rowCount)
            ];
            foreach($this->data[$categoryName] as $k=>$v) {
                getPostsImageUrl($this->data[$categoryName][$k]);
            }
            getResultWithAuthor($this->data[$categoryName]['updates']);
            getResultWithAuthor($this->data[$categoryName]['reads']);
            getResultWithAuthor($this->data[$categoryName]['recomm']);
            shuffle($this->data[$categoryName]['reads']);
            shuffle($this->data[$categoryName]['recomm']);
        }

        $this->data['ratingToons'] = $this->getPostsRankingByRankTypeAndCategoryId('V', getChildrenByParentId(20000), 8);
        getResultWithAuthor($this->data['ratingToons']);
        $this->data['recommendChannel'] = $this->post_model->getPagePostsByPageId(getChildrenByParentId(30000), 8)->get()->result();
    }

    public function series()
    {
        $recentToons = $this->post_model->getPagePostsByCategoryId(getChildrenByParentId(20000), 6)->join('cm_page_attributes pa', 'pp.pageID = pa.pageID', 'left')->get()->result();
        getResultWithAuthor($recentToons);
        $this->data['daynames'] = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
        $this->data['todayToons'] = $recentToons;

        foreach (['recomToons' => [20013, 6], 'faithToons' => [20014, 3], 'wordToons' => [20015, 3]] as $title => list($categoryID, $rowCount)) {
            $this->data[$title] = [
                'updates' => $this->getPostsByCategoryId($categoryID, $rowCount),
                'reads' => $this->getPostsRankingByRankTypeAndCategoryId('V', $categoryID, $rowCount),
                'recomm' => $this->getPostsRankingByRankTypeAndCategoryId('R', $categoryID, $rowCount),
            ];
            getResultWithAuthor($this->data[$title]['updates']);
            getResultWithAuthor($this->data[$title]['reads']);
            getResultWithAuthor($this->data[$title]['recomm']);
            shuffle($this->data[$title]['recomm']);
        }

        $this->data['hotToons'] = $this->getPostsRankingByRankTypeAndCategoryId('V', getChildrenByParentId(20000), 9);
        $this->data['recommLife'] = $this->getPostsRankingByRankTypeAndCategoryId('R', getChildrenByParentId(30000), 3);
        getResultWithAuthor($this->data['hotToons']);
    }

    public function list()
    {
        $this->data['hotToons'] = $this->getPostsRankingByRankTypeAndCategoryId('V', getChildrenByParentId(20000), 9);
        $this->data['recommLife'] = $this->getPostsRankingByRankTypeAndCategoryId('R', getChildrenByParentId(30000), 3);
        getResultWithAuthor($this->data['hotToons']);
    }

    private function getJsonToPostContent($content)
    {
        $html = '';
        $c = json_decode($content);
        foreach ($c as $url) {
            $html .= '<img src="http://crossmap.co.kr' . $url . '">';
        }
        return $html;
    }
}
