<?php

namespace App\Libraries\MyCrawler\Mangboard;

use App\Entities\Mangboard\UserEntity;
use Symfony\Component\DomCrawler\Crawler;

class YamoonCrawler extends MangboardCrawler
{
    public function __construct(string $host, string $board_name, UserEntity $user_entity)
    {
        parent::__construct($host, $board_name, $user_entity);
    }
    //작성내용
    // <div class="panel panel-default">
    //     <div class="text-center panel-heading-local-title text-bold">요즘 화제라는 명품 목걸이</div>
    //     <div style="margin:5px 10px;">
    //         <span class="pull-left dropdown">CAT7478</span>
    //         <span class="pull-right">| 추천 (8) | 조회 (268)</span>
    //         <div class="clearfix"></div>
    //         <hr class="hr-xs-xs">
    //         <span>
    //             <a href="javascript:void(0);" id="incfont"><i class="fa fa-plus fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" id="decfont"><i class="fa fa-minus fa-fw margin-left-5" aria-hidden="true"></i></a>
    //         </span>
    //         <span class="pull-right">2024-09-16 09:52:39</span>
    //         <div class="clearfix"></div>
    //         <hr class="margin-top-5 margin-bottom-20">
    //         <div class="fr-view margin-bottom-30" id="read-content" style="word-break:break-all;">
    //             <p><img title="" class="cloudzoom" data-cloudzoom="zoomImage:'/newboard/yamoonfreeboard/uploads/humor/mceu_18297097311726447898684.webp'" class="fr-fic fr-dii" src="/newboard/yamoonfreeboard/uploads/humor/mceu_18297097311726447898684.webp" alt=""></p>
    //             <p><img title="" class="cloudzoom" data-cloudzoom="zoomImage:'/newboard/yamoonfreeboard/uploads/humor/mceu_41105156321726447902977.png'" class="fr-fic fr-dii" src="/newboard/yamoonfreeboard/uploads/humor/mceu_41105156321726447902977.png" alt=""></p>
    //             <p>&nbsp;</p>
    //             <p>&nbsp;</p>
    //             <p>전화기선 짤라서 목걸이 만들어도 위화감이 전혀 없을것같은</p>
    //             <p>&nbsp;</p>
    //             <p>디자인이군요</p>
    //             <p>&nbsp;</p>
    //         </div>
    //     </div>
    //     <div class="margin-10">
    //             <a href="javascript:void(0)" onclick="javascript:window.open('https://twitter.com/intent/tweet?text='+encodeURIComponent(document.title)+'%20-%20'+encodeURIComponent(document.URL), 'twittersharedialog', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;" target="_blank"> <i class="fa fa-twitter-square fa-lg ya-tooltip" title="트위터 공유하기"></i></a>
    //             <a href="javascript:void(0)" onclick="javascript:window.open('https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(document.URL)+'&t='+encodeURIComponent(document.title), 'facebooksharedialog', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;" target="_blank"> <i class="fa fa-facebook-square fa-lg ya-tooltip" title="페이스북 공유하기"></i></a>
    //     </div>
    //     <div id="freesubframe"></div>
    // </div>
    protected function detail_page(int $cnt, array $listInfo): array
    {
        $response = $this->getMySocket()->getContent("/newboard/yamoonboard/" . $listInfo['detail_url']);
        //작성시간
        // $selector = $this->getSelector($response, getenv("yamoon.view.date.tag"));
        // $listInfo['date'] = trim($selector->text());
        // if ($this->getDebug()) {
        //     throw new \Exception(
        //         sprintf(
        //             "\n--------------%s Debug--------------\n%s\n%s\n---------------------------------------\n",
        //             __FUNCTION__,
        //             var_export($listInfo, true),
        //             $selector->html()
        //         )
        //     );
        // }
        //작성내용
        $tag = getenv("yamoon.view.content.tag");
        $selector = $this->getSelector($response, $tag);
        $media_urls = $this->getUrlsByMediaType($selector, "img",  "src");
        $media_urls = $this->getUrlsByMediaType($selector,  "video",  "src", $media_urls);
        if ($this->getDebug()) {
            throw new \Exception(sprintf(
                "\n--------------%s Debug--------------\n%s%s\n---------------------------------------\n",
                __FUNCTION__,
                var_export($listInfo, true),
                var_export($media_urls, true)
            ));
        } else {
            // Image 나 Video 소스들의 url을 가져와서 실제 다운받는 처리
            $storages = $this->media_process($media_urls);
            if (!count($storages)) {
                throw new \Exception("등록할 자료가 없습니다.");
            }
            $this->backend_process($cnt, $listInfo, $storages);
        }
        return $listInfo;
    }
    //리스트 내용
    // <td class="listvisited mobile-td subject-view">
    // 	<a href="board-read.asp?fullboardname=yamoonfreeboard&mtablename=humor&num=89372&ref=85575&page=1" class="ya-tooltip mobile-bold mobile-height" title="<p><br><br><video autoplay=&quot;autoplay&quot; loop=&quot;loop&quot; muted=&quot;&quot; controls=&quot;controls&quot; width=&quot;560&quot;&quot; height=&quot;  &quot;>        <source src=&quot;   https://files.bepick.net/bbs/2024/09/c2a20ab5771cbb934940551859fce1c8_769966583.mp4 &quot;>    </video><br><br><br></p">
    // 	졸고 있는 여군</a>
    // 		<i class="fa fa-commenting-o" aria-hidden="true"></i> <span class="color-red small">6</span>
    // 	<span class="visible-xs visible-sm small"><i class="fa fa-user-o" aria-hidden="true"></i> yeeyuu | <i class="fa fa-thumbs-o-up" aria-hidden="true"></i> 6 | <i class="fa fa-eye" aria-hidden="true"></i> 369 | No 89372 | 2024-09-13</span>
    // </td>                
    public function execute(int $max_limit): void
    {
        try {
            if ($this->getDebug()) {
                $url = getenv("yamoon.view.test.url.{$this->_board_name}");
                log_message("notice", __FUNCTION__ . "=> DEBUG 게시물 {$url} 작업시작");
                $this->detail_page(1, ['detail_url' => $url]);
                log_message("notice", __FUNCTION__ . "=> DEBUG 게시물 {$url} 작업종료");
            } else {
                $listInfos   = [];
                $response = $this->getMySocket()->getContent(getenv("yamoon.list.url.{$this->_board_name}"));
                $this->getSelector($response, getenv("yamoon.list.tag"))->each(
                    function (Crawler $node) use (&$listInfos): void {
                        $link_node   = $node->filter(getenv("yamoon.list.item.link.tag"));
                        $detail_url  = $link_node->attr("href");
                        $title       = $link_node->text();
                        $info_node   = $node->filter(getenv("yamoon.list.item.info.tag"));
                        $infos       = explode("|", $info_node->text());
                        $listInfos[] = ['title' => $title, 'detail_url' => $detail_url, 'nickname' => trim($infos[0]), 'hit' => trim($infos[2]), 'date' => trim($infos[4])];
                    }
                );
                if (!count($listInfos)) {
                    throw new \Exception("Target URL이 없습니다.");
                }
                $this->main_process($max_limit, $listInfos);
            }
            log_message("notice", __FUNCTION__ . " 작업이 완료되었습니다.");
        } catch (\Exception $e) {
            log_message("warning", sprintf(
                "\n---%s 오류---\n%s\n-----------------------------------------\n",
                __FUNCTION__,
                $e->getMessage()
            ));
        }
    }
}
