<?php defined('BASEPATH') or exit('No direct script access allowed');
use PHPHtmlParser\Dom;

class Cacher
{

    private $CI;

    public function __construct($params = [])
    {
        $this->CI = &get_instance();        
        $this->cache = &$this->CI->cache;
    }

    public function melon()
    {
        $hostUrl = 'https://www.melon.com';
        $path = '/genre/song_list.htm?gnrCode=GN2100';
        return $this->get($hostUrl . $path, '.service_list_song table tbody tr', 'melon', function ($s) {
            preg_match_all('/\d+/', $s->find('.wrap a')[0]->getAttribute('href'), $matches);
            return [
                'img' => $s->find('img')->getAttribute('src'),
                'url' => 'https://www.melon.com/album/detail.htm?albumId=' . $matches[0][0],
                'title' => $s->find('.wrap_song_info a')->innerHtml,
                'album' => $s->find('.wrap_song_info .rank02 a')->innerHtml,
            ];
        });
    }

    public function chtoday($section, $article = null)
    {
        if ($article) {
            return $this->get($article, 'article', null, function ($a) {
                return [
                    'subtitle' => '',
                    'article' => $a->find('.article-body')->innerHtml
                ];
            });
        } else {
            return $this->get('http://www.christiantoday.co.kr/' . $section, '.section-2 article', null, function ($a) {
                return [
                    'img' => $a->find('figure img')->getAttribute('src'),
                    'url' => 'http://www.christiantoday.co.kr' . parse_url($a->find('a')[0]->getAttribute('href'))['path'],
                    'title' => $a->find('h4 a')->innerHtml,
                    'postExcerpt' => $a->find('summary')->innerHtml,
                ];
            });
        }
    }

    public function chdaily($section, $article = null)
    {
        if ($article) {
            return $this->get($article, 'article', null, function ($a) {
                return [
                    'subtitle' => '',
                    'article' => $a->find('section')->innerHtml
                ];
            });
        } else {
            return $this->get('http://www.christianitydaily.com/' . $section, '.news-list-section article', null, function ($a) {
                return [
                    'img' => $a->find('figure img')->getAttribute('src'),
                    'url' => 'http://www.christianitydaily.com' . parse_url($a->find('a')[0]->getAttribute('href'))['path'],
                    'title' => $a->find('h4 a')->innerHtml,
                    'postExcerpt' => $a->find('p')->innerHtml,
                ];
            });
        }
    }

    public function chdailykr($section, $article = null)
    {
        if ($article) {
            return $this->get($article, 'article', null, function ($a) {
                return [
                    'subtitle' => count($a->find('.article-sttl')) ? $a->find('.article-sttl')->innerHtml : '',
                    'article' => $a->find('.article-txt')->innerHtml
                ];
            });
        } else {
            return $this->get('http://www.christiandaily.co.kr/' . $section, '.bk-listbasic1 li', null, function ($a) {
                return [
                    'img' => $a->find('img')->getAttribute('src'),
                    'url' => 'http://www.christiandaily.co.kr' . parse_url($a->find('a')[0]->getAttribute('href'))['path'],
                    'title' => $a->find('h3')->innerHtml,
                    'postExcerpt' => $a->find('p')->innerHtml,
                ];
            });
        }
    }

    

    /**
     * Get remote HTML, parsing and save it in cache file.
     *
     * @param string $url
     * @param string $selecor
     * @param string $cacheName
     * @param callable $fn
     * @return void
     */
    private function get($url, $selecor, $cacheName, $fn)
    {
        if (is_null($cacheName) || !$data = $this->cache->get($cacheName)) {
            $dom = new Dom;
            $dom->loadFromUrl($url);
            $tags = $dom->find($selecor);
            if ($tags) {
                $data = [];
                foreach ($tags as $t) {
                    try {
                        array_push($data, $fn($t));
                    } catch (Exception $e) {
                        continue;
                    }
                }
                if (!is_null($cacheName) && count($data) > 5) {
                    $this->cache->save($cacheName, $data, 60 * 60);
                }
            }
        }
        return $data;
    }

}
