package com.fubaisum.okhttpsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.fubaisum.okhttphelper.OkHttpRequest;
import com.fubaisum.okhttphelper.ThreadMode;
import com.fubaisum.okhttphelper.callback.DownloadCallback;
import com.fubaisum.okhttphelper.params.MultipartParams;
import com.fubaisum.okhttphelper.progress.UiProgressListener;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    private static final String testUrl = "https://raw.githubusercontent.com/fubaisum/AndroidCollections/master/testUser.json";
    private TextView tvTest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvTest = (TextView) findViewById(R.id.tv_test);

        testSyncString();
//        testParseModel();
//        testDownload();
//        testMultipartParams();
    }

    private void testSyncString() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String result = new OkHttpRequest.Builder()
                            .setUrl(testUrl)
                            .get()
                            .build()
                            .string();
                    Log.e("MainActivity", "sync string result = " + result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void testParseModel() {
        new OkHttpRequest.Builder()
                .setUrl("https://github.com/fubaisum/AndroidCollections/testUser.json")
                .build()
                .threadMode(ThreadMode.MAIN)//default
                .callback(new ApiCallback<User>() {

                    @Override
                    protected void onApiSuccess(String msg, User user) {
                        Log.e("MainActivity", user.toString());
                    }

                    @Override
                    protected void onApiFailure(String message) {
                        Log.e("MainActivity", "onResponseFailure : " + message);
                    }

                });
    }

    private void testMultipartParams() {

        String json = "{\"username\":\"abc\"}";

        MultipartParams params = new MultipartParams();
        params.put("token", "fsfaiufy8jn2ir");
        params.putJson("json", json);
        new OkHttpRequest.Builder()
                .setUrl("http://fubaisum.github.io/testUser")
                .post(params)
                .build()
                .threadMode(ThreadMode.MAIN)//default
                .callback(new ApiCallback<User>() {

                    @Override
                    protected void onApiSuccess(String msg, User user) {
                        Log.e("MainActivity", user.toString());
                    }

                    @Override
                    protected void onApiFailure(String message) {
                        Log.e("MainActivity", "onResponseFailure : " + message);
                    }

                });
    }

    private void testDownload() {
        File file = new File(getCacheDir().getAbsolutePath(), "tmp.db");
        new OkHttpRequest.Builder()
//                .setUrl("http://pic41.nipic.com/20140430/18021738_213628575106_2.jpg")
                .setUrl("http://api.youqingjia.com/db/20160614_v1.db")
                .setResponseProgressListener(new UiProgressListener() {
                    @Override
                    public void onUiProgress(long currentBytesCount, long totalBytesCount) {
                        if (totalBytesCount == -1) {
                            tvTest.setText("totalBytesCount is unknown.");
                        } else {
                            float progress = currentBytesCount * 1.0f / totalBytesCount * 100;
                            tvTest.setText("progress = " + progress);
                        }
                    }
                })
                .build()
                .threadMode(ThreadMode.BACKGROUND)
                .callback(new DownloadCallback(file) {
                    @Override
                    public void onResponseSuccess(String fileAbsolutePath) {
                        Log.e("MainActivity", "fileAbsolutePath = " + fileAbsolutePath);
                    }

                    @Override
                    public void onResponseFailure(Exception e) {
                        Log.e("MainActivity", "download onResponseFailure() : " + e);
                    }
                });
    }
}

@@ -315,16 +315,68 @@
 llback;%0A
+import com.fubaisum.okhttphelper.params.FormParams;%0A
 import c
@@ -1050,26 +1050,24 @@
 ncString();%0A
-//
         test
@@ -1076,26 +1076,24 @@
 rseModel();%0A
-//
         test
@@ -1092,24 +1092,31 @@
     test
-Download
+MultipartParams
 ();%0A//  
@@ -1121,39 +1121,32 @@
         test
-MultipartParams
+Download
 ();%0A    %7D%0A%0A 
@@ -1861,122 +1861,282 @@
     
-new OkHttpRequest.Builder()%0A                .setUrl(%22https://github.com/fubaisum/AndroidCollections/testUser.json%22
+User user = new User();%0A        user.name = %22abc%22;%0A        user.address = %22unknown%22;%0A        FormParams params = new FormParams();%0A        params.put(%22user%22, User.class, user);%0A        new OkHttpRequest.Builder()%0A                .setUrl(testUrl)%0A                .post(params
 )%0A  
@@ -2754,27 +2754,135 @@
 %22%7B%5C%22
-username%5C%22:%5C%22abc%5C%22%7D
+name%5C%22:%5C%225555555555555555555555%5C%22%7D%22;%0A        User user = new User();%0A        user.name = %22abc%22;%0A        user.address = %22unknown
 %22;%0A%0A
@@ -3022,16 +3022,117 @@
  json);%0A
+        params.put(%22user%22, User.class, user);%0A        params.putJson(%22userJson%22, User.class, user);%0A%0A
         

update sample