<?php namespace Client\Request;

class Repo extends Base {
    
    public $rid;
    public $rowner;
    public $rname;
    public $rfull_name;
    public $rdescription;
    public $rprivate;
    public $rfork;
    public $rparent;
    public $rempty;
    public $rmirror;
    public $rsize;
    public $rhtml_url;
    public $rssh_url;
    public $rclone_url;
    public $rwebsite;
    public $rstars_count;
    public $rforks_count;
    public $rwatchers_count;
    public $ropen_issues_count;
    public $rdefault_branch;
    public $rcreated_at;
    public $rupdated_at;
    public $rpermissions;
    public $radmin;
    public $rpush;
    public $rpull;

    protected $owner;

    public function __construct(string $api_url, string $api_token, User $owner = null, string $name = null) {
        $this->rowner = $owner;
        $this->rname = $name;
        parent::__construct($api_url, $api_token);
    }

    protected function set_scope(string $method) {
        switch ($method) {
        case "create":
            $this->scope = "/user" . ($this->rowner->authenticated() ? "" : "/" . $this->rowner->uusername) . "/repos";
            break;
        case "delete":
            $this->scope = "/repos/" . $this->rowner->uusername . "/" . $this->rname;
            break;
        default:
            $this->scope = "/repos/" . ($this->rowner ? $this->rowner->uusername . "/" . $this->rname : $this->rfull_name);
        }
    }

    protected function json_set_property($obj) {
        foreach($obj as $key => $val) {
            $key = 'r' . $key;
            if (property_exists($this, $key)) {
                switch ($key) {
                case 'rowner':
                    if (!$this->rowner) {
                        $user = new User($this->url, $this->token);
                        $user->json_set_property($val);
                        $this->{$key} = $user;
                    }
                    break;
                default:
                    $this->{$key} = $val;
                }
            }
        }
        $this->loaded = true;
    }

    public function search(string $q) {
        $searchable = sprintf("%s %s", $this->rname, $this->rdescription);

        return stripos($searchable, $q) !== false;
    }

    public function create(...$args) {

        if ($this->loaded)
            return false;

        $params = array(
            "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
            "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
            "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
            "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
            "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
            "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
            "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
        );

        $params = array_filter($params, function($val) {
            return $val != null;
        });

        parent::create($params);
    }
}