package com.messners.gitlab.api;

import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;

import com.messners.gitlab.api.models.RepositoryFile;


/**
 * This class provides an entry point to all the GitLab API repository files calls.
 * 
 * @author lonfee88 <lonfee88@gmail.com>
 */
public class RepositoryFileApi extends AbstractApi {

	public RepositoryFileApi (GitLabApi gitLabApi) {
		super(gitLabApi);
	}
	
    /**
     * Get file from repository
     * Allows you to receive information about file in repository like name, size, content.
     * Note that file content is Base64 encoded. 
     * 
     * GET /projects/:id/repository/files
     * 
     * @param filePath
     * @param projectId
     * @param ref
     * @return
     * @throws GitLabApiException
     */
	public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException {   
        Form form = new Form();
        addFormParam(form, "file_path", filePath, true);
        addFormParam(form, "ref", ref, true);
        Response response = get(Response.Status.OK, form.asMap(),"projects", projectId, "repository", "files");
        return (response.readEntity(RepositoryFile.class));
    }
	
	/**
	 * Create new file in repository
	 * 
	 * POST /projects/:id/repository/files
	 * 
	 * @param file
	 * @param projectId
	 * @param branchName
	 * @param commitMessage
	 * @return
	 * @throws GitLabApiException
	 */
	public RepositoryFile createFile (RepositoryFile file, Integer projectId, String branchName,  String commitMessage) throws GitLabApiException {
	    Form formData = file2form(file, branchName, commitMessage);
        Response response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files");
        return (response.readEntity(RepositoryFile.class));
    }
	
	/**
	 * Update existing file in repository
	 * 
	 * PUT /projects/:id/repository/files
	 * 
	 * @param file
	 * @param projectId
	 * @param branchName
	 * @param commitMessage
	 * @return
	 * @throws GitLabApiException
	 */
	public RepositoryFile updateFile (RepositoryFile file, Integer projectId, String branchName,  String commitMessage) throws GitLabApiException {
	    Form form = file2form(file, branchName, commitMessage);     
        Response response = put(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "files");
        return (response.readEntity(RepositoryFile.class));
    }
	
	/**
	 * Delete existing file in repository
	 * 
	 * DELETE /projects/:id/repository/files
	 *
	 * @param filePath
	 * @param projectId
	 * @param branchName
	 * @param commitMessage
	 * @throws GitLabApiException
	 */
	public void deleteFile (String filePath, Integer projectId, String branchName,  String commitMessage) throws GitLabApiException {
        
        if (filePath == null) {
            throw new RuntimeException("filePath cannot be null");
        }
        
        Form form = new Form();
        addFormParam(form, "file_path", filePath, true);
        addFormParam(form, "branch_name", branchName, true);
        addFormParam(form, "commit_message", commitMessage, true);
        delete(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "files");
    }
	
	private Form file2form(RepositoryFile file, String branchName, String commitMessage){
	    Form form = new Form();
	    addFormParam(form, "file_path", file.getFilePath(), true);
	    addFormParam(form, "branch_name", branchName, true);
	    addFormParam(form, "encoding", file.getEncoding(), false);
	    addFormParam(form, "content", file.getContent(), true);
	    addFormParam(form, "commit_message", commitMessage, true);
	    return form;
	}
	
}

@@ -676,54 +676,155 @@
 file
-Path%0A     * @param projectId%0A     * @param ref
+_path (required) - Full path to new file. Ex. lib/class.rb%0A     * @param projectId%0A     * @param ref (required) - The name of branch, tag or commit
 %0A   
@@ -1383,32 +1383,312 @@
 tory/files%0A%09 * %0A
+%09 * file_path (required) - Full path to new file. Ex. lib/class.rb%0A     * branch_name (required) - The name of branch%0A     * encoding (optional) - 'text' or 'base64'. Text is default.%0A     * content (required) - File content%0A     * commit_message (required) - Commit message%0A%09 * %0A
 %09 * @param file%0A
@@ -2285,24 +2285,307 @@
 /files%0A%09 * %0A
+%09 * file_path (required) - Full path to new file. Ex. lib/class.rb%0A     * branch_name (required) - The name of branch%0A     * encoding (optional) - 'text' or 'base64'. Text is default.%0A     * content (required) - File content%0A     * commit_message (required) - Commit message%0A     * %0A
 %09 * @param f
@@ -3191,16 +3191,188 @@
 les%0A%09 *%0A
+%09 * file_path (required) - Full path to file. Ex. lib/class.rb%0A     * branch_name (required) - The name of branch%0A     * commit_message (required) - Commit message%0A     * %0A
 %09 * @par

add more comments.