<?php

/*
 * TODO:
 *
 * Rewrite this. Shouldnt be a test file, like its now.
 *
 * */

require "./src/gpac.php";

use Gogs\Lib\Curl\Exception as ApiException;

define('API_URL', 'https://git.giaever.org/api/v1');
//define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
define("API_TOKEN", 'e14b9eff0749b6f0c4cadf4bb72b83d44578ae');


try {
    $client =  new Gogs\API\Client(API_URL, API_TOKEN);

    $me = $client->user()->load();

    $user_search = $client->users()->search(array(
        "name" => "tester2"
    ));

    if ($user_search->len() > 0)
        echo " * Found user(s) " . var_export($user_search, true);
    else
        echo " * User 'tester2' not found"; 

    try {

        echo " * Creating new repo under autorized user: " . $me->username;
        // Create new repo under authorized user
        $repo = $me->repos()->create(
            /*name*/        "gogs-php-api-client-test", 
            /*desc*/        "Repository created from test file.",
            /*private*/     false,
            /*auto init*/   true,
            /*git ignore*/  "Vim",
            /*license*/     "MIT License"
            /* default read me */
        );
        
       echo "New repo '" . $repo->full_name . "' created!\n";

    } catch(ApiException\HTTPUnexpectedResponse $e) {

        // Repo exists, get it!
        $repo = $me->repo("gogs-php-api-client-test");

        // Delete it (it will now be created again on reload!)
        $repo->delete();

        echo "Repo '" . $repo->full_name . "' deleted\n";
    }

    // Loop through organizations
    foreach($me->organizations()->load()->all() as $org)
        var_dump("ORG", $org->username);

    echo sprintf(
        "org->username: %s\norg->org_username: %s\norg->user_username: %s\n",
        $org->username, $org->org_username, $org->user_username
    ); 

    // Requires ADMIN rights to the authenticated user
    #try {
        // Create new user "tester2"
    #    $user = $client->users()->new("tester2", "git@git.giaever.org");

    #    echo "New user '" . $user->uusername . "' created!\n";

        // And a new repository under this user
    #    $repo = $user->repos()->new(
    #        /*name*/        "gogs-php-api-client-auth-test", 
    #        /*desc*/        "Repository created from test file.",
    #        /*private*/     false,
    #        /*auto init*/   true,
    #        /*git ignore*/  "Vim",
    #        /*license*/     "MIT License"
    #        /* default read me */
    #    );
           
    #    echo "New repo '" . $repo->rfull_name . "' created\n";

    #    $org = $user->organizations()->new(
    #        /*username*/ "tester-org",
    #        /*full name*/ "Tester's organization",
    #        /*desc*/ "Just a test organization",
    #        /*website*/ "https://git.giaever.org/",
    #        /*location*/ "Norway"
    #    );

    #}catch (\Lib\NotAuthorizedException $e) {
    #    echo "User (me) '" . $me->uusername . "' not authorized for this action: " . $e->getMessage();
    #} catch (\Lib\HTTPUnexpectedResponse $e) {
    
        // Most likely user exist; then delete it
    #    $user = $client->users()->get("tester2");

        // Delete it's repositories
    #    foreach ($user->repos()->load()->all() as $repo)
    #        $repo->delete();

    #    foreach ($user->organizations()->load()->all() as $org)
    #        $org->delete();

    #    $user->delete();
    #    echo "User '" . $user->uusername . "' deleted!\n";
    #}

    /*var_dump($me->organizations()->load()->all()["FlyViking"]->repos()->create(
        "test-repo-for-org",
        "this is desc."
    ));*/

} catch (ApiException\NotAuthorizedException $e) {
    die("NOT AUTH: " . $e->getMessage());
} catch (ApiException\HTTPUnexpectedResponse $e) {
    die($e);
} catch (Exception $e) {
    die($e);
}
?>