<?php

namespace DetailTask;

use LogHelper;
use SekolahNewClient;

/**
 * @property SekolahNewClient $client
 */
class SekolahTask extends AbstractTask {
    #region Attributes
    public const TYPE_INIT = "init";
    public const TYPE_DAILY = "daily";
    #endregion

    #region Main
    public function init() {
        $this->client = new SekolahNewClient($this->config["sekolah_new_prop"]);
        $this->client->setLogger($this->logger);
    }

    /**
     * @param $type
     * @param $debug
     * @return void
     * Logging disimpan ke table log_sekolah_errors
     */
    public function processSekolah($type, $debug) {
        $this->init();
        if ($type == self::TYPE_INIT) {
            $this->processSekolahInit();
        } else if ($type == self::TYPE_DAILY) {
            $this->processSekolahDaily($debug);
        }
    }

    private function processSekolahInit() {
        $this->processName = "Process Sekolah Init";
        $startTime = microtime(true);
        $this->client->init();
        $endTime = microtime(true);
        LogHelper::logInfo($this->processName, $startTime, $endTime, [], self::LOG_TYPE, $this->logger);
    }

    private function processSekolahDaily($debug) {
        $this->processName = "Process Sekolah Daily";
        $startTime = microtime(true);
        $this->client->updateDaily($debug);
        $endTime = microtime(true);
        LogHelper::logInfo($this->processName, $startTime, $endTime, ["objects_processed" => $this->client->getObjectsProcessed()], self::LOG_TYPE, $this->logger);
    }

    private function logError($message, $additionalParam = "")
    {
        $param = "page=".$this->page."&limit=".$this->limit.$additionalParam;
        $this->logApiError("ScheduledTaskProcessRumahSakit", $param, $message);
    }
    #endregion
}
