
package com.amihaiemil.versioneye;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.json.JsonArray;
import javax.json.JsonObject;
import com.jcabi.http.Request;
import com.jcabi.http.response.JsonResponse;
import com.jcabi.http.response.RestResponse;

/**
 * Real implementation of {@link Project}.
 * @author Mihai Andronache (amihaiemil@gmail.com)
 * @version $Id$
 * @since 1.0.0
 */
final class RtProject implements Project {

    /**
     * Project's team.
     */
    private Team team;
    
    /**
     * HTTP request.
     */
    private Request req;
    
    /**
     * This project as Json.
     */
    private JsonObject project;

    /**
     * Ctor.
     * @param req HTTP request for this project.
     * @param team This project's team.
     * @param project This project as Json.
     */
    RtProject(final Request req, final Team team, final JsonObject project){
        this.req = req;
        this.team = team;
        this.project = project;
    }
    
    @Override
    public Team team() {
        return this.team;
    }

    @Override
    public Organization organization() {
        return this.team.organization();
    }

    @Override
    public void delete() throws IOException {
        this.req.uri().path(this.project.getString("ids", ""))
            .back().method("DELETE").fetch().as(RestResponse.class)
            .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
    }

    @Override
    public Project update(final File projectFile) throws IOException {
        return new RtProject(
            this.req, this.team,
            this.req.uri().path(this.project.getString("ids", "")).back()
                .method("POST").multipartBody()
                .formParam("project_file", projectFile).back()
                .fetch().as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_CREATED)
                .as(JsonResponse.class)
                .json().readObject()
        );
    }

    @Override
    public String projectId() {
        return this.project.getString("ids");
    }

    @Override
    public String name() {
        return this.project.getString("name");
    }

    @Override
    public String type() {
        return this.project.getString("project_type", "");
    }

    @Override
    public boolean publicProject() {
        return this.project.getBoolean("public", false);
    }

    @Override
    public boolean privateScm() {
        return this.project.getBoolean("private_scm", false);
    }

    @Override
    public String period() {
        return this.project.getString("period", "");
    }

    @Override
    public String source() {
        return this.project.getString("source", "");
    }

    @Override
    public int dependencies() {
        return this.project.getInt("dep_number", 0);
    }

    @Override
    public int outdated() {
        return this.project.getInt("out_number", 0);
    }

    @Override
    public int licensesUnknown() {
        return this.project.getInt("licenses_unknown", 0);
    }

    @Override
    public int licensesRed() {
        return this.project.getInt("licenses_red", 0);
    }

    @Override
    public String whitelist() {
        return this.project.getString("license_whitelist_name", "");
    }

    @Override
    public List<String> childIds() {
        final List<String> children = new ArrayList<>();
        final JsonArray ids = this.project.getJsonArray("child_ids");
        for(int idx = 0; idx< ids.size(); idx++) {
            children.add(ids.getString(idx));
        }
        return children;
    }

    @Override
    public String parentId() {
        return this.project.getString("parent_id", "");
    }

    @Override
    public String createdAt() {
        return this.project.getString("created_at", "");
    }

    @Override
    public String updatedAt() {
        return this.project.getString("updated_at", "");
    }

    @Override
    public JsonObject json() {
        return this.project;
    }

    @Override
    public Project hook() throws IOException {
        ((RtVersionEye) this.team.versionEye())
            .request().uri()
            .path("/github/hook/")
            .path(this.projectId())
            .back()
            .method("POST")
            .fetch()
            .as(RestResponse.class)
            .assertStatus(HttpURLConnection.HTTP_CREATED);
        return this;
    }

}
