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

class Pray extends MY_Controller
{
    private $sections;

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

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

    private function desktop()
    {
        $section = $this->uri->segment(2);
        $article = $this->uri->segment(3);
        $this->data['sectionCategory'] = NEWS_SECTION_CATEGORY;
        if (is_numeric($article)) {
            $this->article($article);
            $this->load->view('templates/pray/pray-article', $this->data);
        } elseif ($section) {
            if ($this->section()) {
                $this->load->view('templates/pray/pray-' . $section, $this->data);
            } else {
                $this->{$section}();
            }
        } else {
            $this->main();
            $this->load->view('templates/pray/pray-home', $this->data);
        }
    }

    private function mobile()
    {
        $section = $this->uri->segment(2);
        $article = $this->uri->segment(3);
        if (is_numeric($article)) {
            $this->load->view('mobile/pray/pray-article', $this->data);
        } else if ($article) {
            $this->load->view('mobile/pray/pray-editor', $this->data);
        } elseif ($section) {
            $this->load->view('mobile/pray/pray-section', $this->data);
        } else {
            $this->load->view('mobile/pray/pray-home', $this->data);
        }
    }

    private function main()
    {
        $this->setSections();
        $this->data['prayon'] = $this->pray_model->getPaging(15, 0)
            ->get()->result();
        $this->data['posts'] = $this->post_model->getPostsByCategoryId(
            array_column($this->category_model->getCategoriesBySlug([
                'vclip', 'together', 'story', 'art'
            ])->get()->result(), 'categoryID'),
            5
        )->get()->result();
        $this->data['featured'] = $this->data['posts'][0];
        foreach ($this->data as $k => $v) {
            getPostsImageUrl($this->data[$k]);
        }
    }


    private function getCategoryIdBySlug($slug)
    {
        return $this->category_model->getCategoriesBySlug($slug)->get()->row()->categoryID;
    }

    private function getPostsByCategorySlug($slug)
    {
        return $this->post_model->getPostsByCategoryId($this->getCategoryIdBySlug($slug), 20);
    }

    private function section()
    {
        $this->setSections();
        foreach ($this->data as $k => $v) {
            getPostsImageUrl($this->data[$k]);
        }
        if (isset($this->sections[$this->uri->segment(2)])) {
            $this->data['posts'] = &$this->data[$this->uri->segment(2)];
            return true;
        }
    }

    private function article($article)
    {
        $this->load->model('comment_model');
        $this->data['comments'] = $this->comment_model->getByPostId($article)
            ->where('parentID', 0)
            ->join('cm_user', 'cm_comment.userID=cm_user.userID', 'left')
            ->select('*, 0 like, 0 bad, 0 replies')->get()->result();
        $this->comment_model->getRepliesAndRankings($this->data['comments']);
        $this->data['post'] = $this->post_model->getPost($article);
        foreach ($this->data as $k => $v) {
            getPostsImageUrl($this->data[$k]);
        }
    }


    private function setSections()
    {
        $this->sections = [
            'prayon' => ['', function () {
                $this->data['prayon'] = array_reverse($this->pray_model->getPaging(15, 0)->select('py.*, 0 bookmark, 0 like, 0 bad, 0 bookmarks')
                    ->get()->result());
                $this->pray_model->getBookmarksAndRankings($this->data['prayon']);
            }],
            'vclip' => ['vclip', function () {
                $this->data['vclip'] = $this->getPostsByCategorySlug($this->sections['vclip'][0])
                    ->join('cm_post_video v', 'p.postID=v.postID', 'left')
                    ->join('cm_user u', 'p.userID=u.userID', 'left')
                    ->select("if(u.nickname = '' or u.nickname is null, u.userName, u.nickName) as author")
                    ->get()->result();
            }],
            'together' => ['together', function () {
                $this->data['pages'] = [];
                $this->data['together'] = $this->post_model->getPagePostsByCategoryId($this->getCategoryIdBySlug($this->sections['together'][0]), 29)
                    ->join('cm_user u', 'p.userID=u.userID', 'left')
                    ->select("*,if(u.nickname = '' or u.nickname is null, u.userName, u.nickName) as author, p.postID postSeq")
                    ->get()->result();
            }],
            'story' => ['story', function () {
                $this->data['story'] = $this->getPostsByCategorySlug($this->sections['story'][0])
                    ->join('cm_user u', 'p.userID=u.userID', 'left')
                    ->select("if(u.nickname = '' or u.nickname is null, u.userName, u.nickName) as author")
                    ->get()->result();
            }],
            'art' => ['art', function () {
                $this->data['art'] = $this->getPostsByCategorySlug($this->sections['art'][0])
                    ->join('cm_user u', 'p.userID=u.userID', 'left')
                    ->select("if(u.nickname = '' or u.nickname is null, u.userName, u.nickName) as author")->get()->result();
            }]
        ];

        foreach ($this->sections as $k => $s) {
            $s[0] || $this->uri->segment(2) == $k ? $s[1]() : '';
        }
    }

    public function rating()
    {
        $data = [
            'prayID' => $this->input->post('prayId'),
            'userID' => getSessionUser()->is ? getSessionUser()->userID : 0,
            'title' => $this->input->post('title'),
            'dateUpdated' => date('Y-m-d H:i:s')
        ];
        if ($insertId = $this->pray_model->bookmark($data)) {
            $this->response(array('code' => 200, 'rating' => 0));
            // $this->response(array('code' => 200, 'rating' => $this->pray_model->getPrayRankingCountByTitle($data['prayID'], $data['title'])));
        } else {
            $this->response(array('code' => 500, 'error' => '서버오류가 발생했습니다'));
        }
    }
}
