<?php

namespace App\Services\TG\Hook\Mod;
use App\Services\TG\Hook\Dog;
use App\Models\Article as ModelArticle;
use App\Services\TomTool\Http;
use App\Services\TG\Http as TGHttp;

class Hub extends Base {
    
    private $oDog;

    public function __construct($aUpdate = [])
    {
        
        $this->oDog = new Dog(new ModelArticle());
        $this->oDog->_setDogName("hub"); // 必须
//        $this->oDDT->_setPrimaryKey("id"); // 可选，默认id
//        $this->oDDT->_setStrField(["content"]); // 可选，指定长文本字段
//        $this->oDog->_setInFormat(["user_name"]);
//        $this->oDog->_setOutFormat(["user_name" => "date"]);
        
        $this->oHttp = new Http();
        $this->oTGHttp = new TGHttp();
        
    }
    
    public function __call($sName, $aArg)
    {
        
        return $this->oDog->$sName($aArg[0]);
        
    }
    
    public function github($aParam)
    {
//        $sWhereField = $aParam["aOption"] ?? $this->
        $iId = $aParam["aArg"][1] ?? false;
        
        if (empty($aParam["aOption"]) && empty($aParam["aArg"])) {
            return "/dog#github {id}";
        }
        
        $oArticle = ModelArticle::find($iId);
        
        $this->oHttp->sToUrl = "https://api.github.com/repos/".$oArticle->user_name."/".$oArticle->project_name;
        $this->oHttp->sMetHod = "get";
        $r = $this->oHttp->send();
        
        $aGitHub = json_decode($r, true);
        
        if (!isset($aGitHub["id"])) {
            $aGitHub["thisSendUrl"] = $this->oHttp->sToUrl;
            return $this->toJson($aGitHub);
        }
        
        $tmp = str_replace("T", " ", rtrim($aGitHub["updated_at"], "Z")); //updated_at, pushed_at
        $tmp = strtotime($tmp);
        $tmpDate = date("Y-m-d H:i:s", $tmp + (8 * 3600));
        $tmpTime = strtotime($tmpDate);
        $tmpNowTime = time();
        $tmpDiff = $tmpNowTime - $tmpTime;
        $iLastUpdate = ceil($tmpDiff / 60);
        
        $aGitHubNew = [];
        $aGitHubNew["⭐️星星"] = $aGitHub["stargazers_count"];
        $aGitHubNew["♻️最后更新"] = $iLastUpdate."分钟前"; // 替换T为空格并去掉Z
        $aGitHubNew["watchers"] = $aGitHub["watchers_count"];
        $aGitHubNew["forks"] = $aGitHub["forks_count"];
        $aGitHubNew["watchers"] = $aGitHub["watchers_count"];
        $aGitHubNew["url"] = $this->urlFormat($aGitHub["html_url"]);
        
        return $this->toJson($aGitHubNew);
        
    }
    
//    public function JsonAdd($aParam) {
//        
//        return $this->JsonSet($aParam, "add");
//        
//    }
    
//    public function Json($aParam)
//    {
//        $iId = $aParam["aOption"][1] ?? false;
//        $sJson = $aParam["aArg"] ?? false;
//        
//        if (empty($aParam["aOption"]) && empty($aParam["aArg"])) {
//            return "/article#json__id? <json> | 有id为set，无id为add";
//        }
//        
//        
////        if (!$iId) {
////            return "需要id";
////        }
////
////        if (!$sJson) {
////            return "需要json";
////        }
//        
//        if (!$iId) {
//            
//            $oArticle = new ModelArticle();
//            
//        } else {
//            
//            $oArticle = ModelArticle::find($iId);
//            if (!$oArticle) {
//                return "没到到这个";
//            }
//            
//        }
//        
//        $sJson = implode(" ", $sJson);
//        
//        $aJson = json_decode($sJson, true);
//        
//        foreach ($aJson as $k => $v) {
//            $oArticle->$k = $v;
//        }
//        
//        $oArticle->save();
//        
//        $aParam["aArg"][1] = $oArticle->id;
//        return $this->Find($aParam);
//    }
    
}
