<?php

namespace App\Controllers\Mangboard;

use App\Libraries\MyCrawler\YamapCrawler;
use App\Controllers\CommonController;
use App\Libraries\Mangboard\UserLibrary;

class CrawlerController extends CommonController
{
    public function yamap(string $category, string $id = "", string $debug = "false"): string
    {
        try {
            $id = $id == "" ? getenv("mangboard.login.default.id") : $id;
            $password = getenv("mangboard.login.default.password");
            //1. 사이트 로그인 처리            
            $user_library = new UserLibrary();
            $user_entity = $user_library->login(getenv("mangboard.host.url"), $id, $password);
            //2. 필요한 로그인한 사용자정보,Socket,Storage 정의후 Crawler에게 전달.
            $crawler = new YamapCrawler($category);
            $crawler->setUserEntity($user_entity);
            $crawler->setDebug($debug === "true" ? true : false);
            $crawler->execute();
            return "완료되었습니다.";
        } catch (\Exception $e) {
            log_message("error",  $e->getMessage());
            return $e->getMessage();
        }
    }
}
