package controllers;

import play.*;
import play.mvc.*;
import play.libs.Json;
import play.data.*;
import play.libs.*;
import java.util.Map;
import java.util.HashMap;
import views.html.*;

import static play.mvc.Results.*;

import play.data.validation.*;
import static play.data.validation.Constraints.*;
import javax.validation.*;

import util.*;

import org.joda.time.LocalTime;

import models.*;

import com.qiniu.api.auth.digest.Mac;

public class Storage extends Controller {
    public static Result uploadToken() {
        Map<String,String[]> queryString = request().queryString();
        // this is demo, user get from query
        String user = queryString.get("user")[0];

        String bucket= Play.application().configuration().getString("qiniu.bucket");
        String callbackUrl = Play.application().configuration().getString("qiniu.callback_url");

        String token = util.Qiniu.uploadToken(bucket, user, callbackUrl);
        if (token == null) {
            return badRequest(
                util.Error.Invalid.toJson()
            );
        }

        Map<String, String> resp = new HashMap<String, String>();
        resp.put("token", token);
        return ok(
            Json.toJson(resp)
        );
    }

    public static Result downloadToken() {
        Map<String,String[]> queryString = request().queryString();
        String key = queryString.get("key")[0];
        // this is demo, user get from query
        String user = queryString.get("user")[0];

        File f = File.find.byId(key);
        if (!f.user.equals(user)) {
            return badRequest(
                util.Error.Forbiden.toJson()
            );
        }

        String downTokenUrl = downloadUrl(key);
        Map<String, String> resp = new HashMap<String, String>();
        resp.put("url", downTokenUrl);
        return ok(
            Json.toJson(resp)
        );
    }

    private static String downloadUrl(String key){
        String domain= Play.application().configuration().getString("qiniu.domain");
        String downloadUrl = "http://"+ domain + "/" + key;
        return util.Qiniu.downloadToken(downloadUrl);
    }

    public static Result downloadTokens(FileCollection col){
        // get files from collection
        // iterate files to get tokens
        // return download token url array
        return null;
    }

    public static Result delete() {

        Map<String,String[]> queryString = request().queryString();
        String bucket= Play.application().configuration().getString("qiniu.bucket");
        String key = queryString.get("key")[0];
        // to do find, and check owner
        // File = File.Find()

        try {
            util.Qiniu.delete(bucket, key);
        } catch(Exception e){
            e.printStackTrace();
            return badRequest(Json.toJson(null));
        }
        return ok(
            Json.toJson(null)
        );
    }


    private static Form<File> upCallbackForm = Form.form(File.class);
    //bucket, key
    public static Result callback() {
        Form<File> boundCallback = upCallbackForm.bindFromRequest();
        if (boundCallback.hasErrors()) {
            return badRequest();
        }

        File data = boundCallback.get();

        Logger.info("callback done"+ data.user);
        data.timestamp = LocalTime.now();
        data.save();
        Map<String, String> resp = new HashMap<String, String>();
        resp.put("success", "true");
        return ok(
            Json.toJson(resp)
        );
    }
}

@@ -620,32 +620,179 @@
  get from query%0A
+        if (queryString.get(%22user%22) == null) %7B%0A            return badRequest(%0A                util.Error.Invalid.toJson()%0A            );%0A        %7D%0A
         String u
@@ -817,32 +817,32 @@
 get(%22user%22)%5B0%5D;%0A
-
 %0A        String 
@@ -1483,32 +1483,213 @@
 .queryString();%0A
+        if (queryString.get(%22key%22) == null %7C%7C queryString.get(%22user%22) == null) %7B%0A            return badRequest(%0A                util.Error.Invalid.toJson()%0A            );%0A        %7D%0A
         String k
@@ -1905,34 +1905,33 @@
      return 
-badRequest
+forbidden
 (%0A          
@@ -3355,24 +3355,60 @@
 allback() %7B%0A
+        Logger.info(%22callbacking%22);%0A
         Form
@@ -3587,24 +3587,24 @@
 ack.get();%0A%0A
-
         Logg
@@ -3625,16 +3625,17 @@
 ack done
+ 
 %22+ data.

add auth