/**
 * Android Jungle-Easy-Http demo framework project.
 *
 * Copyright 2016 Arno Zhang <zyfgood12@163.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jungle.easyhttp.demo;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jungle.easyhttp.model.base.BizModelListener;
import com.jungle.easyhttp.model.base.ModelLoadLifeListener;
import com.jungle.easyhttp.model.base.ModelMethod;
import com.jungle.easyhttp.model.binary.DownloadFileRequestModel;
import com.jungle.easyhttp.model.binary.DownloadRequestModel;
import com.jungle.easyhttp.model.binary.UploadRequestModel;
import com.jungle.easyhttp.model.text.JsonRequestModel;
import com.jungle.easyhttp.model.text.TextRequestModel;
import com.jungle.easyhttp.network.HttpsUtils;
import com.jungle.easyhttp.request.EasyHttpManager;
import com.jungle.easyhttp.request.queue.HttpsRequestQueueFactory;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private static final String DEMO_WEB_URL =
            "https://github.com/arnozhang/easy-http/blob/master/.gitignore";

    private static final String DEMO_JSON_URL =
            "https://raw.githubusercontent.com/arnozhang/easy-http/master/docs/demo.json";

    private static final String DEMO_UPLOAD_URL =
            "https://raw.githubusercontent.com/upload_test";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        HttpsRequestQueueFactory factory = new HttpsRequestQueueFactory(
                this, "github.cer", "githubusercontent.cer");
        factory.setHostnameVerifier(new HttpsUtils.DomainHostNameVerifier(
                "github.com", "githubusercontent.com"));
        EasyHttpManager.getInstance().setRequestQueueFactory(factory);
    }

    private void showToast(int errorCode, String message) {
        message = String.format("Error: errorCode = %d, message = %s.", errorCode, message);
        Log.e("Main", message);
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    private Context getContext() {
        return this;
    }

    public void onTextModel(View view) {
        TextRequestModel
                .newModel()
                .url(DEMO_WEB_URL)
                .method(ModelMethod.GET)
                .loadWithProgress(this, new BizModelListener<String>() {
                    @Override
                    public void onSuccess(Map<String, String> headers, String response) {
                        TextViewerActivity.start(getContext(), response);
                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        showToast(errorCode, message);
                    }
                });
    }

    public void onJsonModel(View view) {
        JsonRequestModel
                .newModel(GithubUserInfo.class)
                .url(DEMO_JSON_URL)
                .method(ModelMethod.GET)
                .load(new BizModelListener<GithubUserInfo>() {
                    @Override
                    public void onSuccess(Map<String, String> headers, GithubUserInfo response) {
                        String info = JSON.toJSONString(response, SerializerFeature.PrettyFormat);
                        info = "Load Json object success!\n\n" + info;
                        TextViewerActivity.start(getContext(), info);
                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        showToast(errorCode, message);
                    }
                });
    }

    public void onDownloadModel(View view) {
        DownloadRequestModel
                .newModel()
                .url(DEMO_JSON_URL)
                .method(ModelMethod.GET)
                .loadWithProgress(this, new BizModelListener<byte[]>() {
                    @Override
                    public void onSuccess(Map<String, String> headers, byte[] response) {
                        String content = new String(response);
                        TextViewerActivity.start(getContext(), content);
                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        showToast(errorCode, message);
                    }
                });
    }

    private String getDemoFilePath() {
        return Environment.getExternalStorageDirectory().getPath() + "/demo.json";
    }

    public void onDownloadFileModel(View view) {
        final String file = getDemoFilePath();
        String loadingText = String.format("Downloading File: \n%s", file);

        DownloadFileRequestModel
                .newModel()
                .url(DEMO_JSON_URL)
                .filePath(file)
                .lifeListener(new ModelLoadLifeListener<DownloadFileRequestModel>() {
                    @Override
                    public void onBeforeLoad(DownloadFileRequestModel model) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                100);
                    }
                })
                .loadWithProgress(getContext(), loadingText, new BizModelListener<String>() {
                    @Override
                    public void onSuccess(Map<String, String> headers, String response) {
                        Toast.makeText(getContext(), String.format(
                                "Download file SUCCESS! file = %s.", file), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        showToast(errorCode, message);
                    }
                });
    }

    public void onUploadFileModel(View view) {
        final String file = getDemoFilePath();

        UploadRequestModel
                .newModel()
                .url(DEMO_UPLOAD_URL)
                .addFormItem(file)
                .loadWithProgress(this, "Uploading...", new BizModelListener<String>() {
                    @Override
                    public void onSuccess(Map<String, String> headers, String response) {
                        Toast.makeText(getContext(), String.format(
                                "Upload file SUCCESS! file = %s.", file), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        showToast(errorCode, message);
                    }
                });
    }
}

@@ -1866,61 +1866,21 @@
 s://
-git
+www.zhi
 hu
-b
 .com
-/arnozhang/easy-http/blob/master/.gitignore
 %22;%0A%0A
@@ -2308,72 +2308,125 @@
     
-HttpsRequestQueueFactory factory = new HttpsRequestQueueFactory(
+String%5B%5D certs = %7B%0A                %22zhihu.cer%22,%0A                %22github.cer%22,%0A                %22githubusercontent.cer%22
 %0A   
@@ -2430,24 +2430,28 @@
         
+%7D;%0A%0A
         
 this, %22g
@@ -2446,51 +2446,223 @@
     
-this, %22github.cer%22, %22githubusercontent.cer%22
+String%5B%5D domains = %7B%0A                %22zhihu.com%22,%0A                %22github.com%22,%0A                %22githubusercontent.com%22%0A        %7D;%0A%0A        HttpsRequestQueueFactory factory = new HttpsRequestQueueFactory(this, certs
 );%0A 
@@ -2738,62 +2738,15 @@
 ier(
-%0A                %22github.com%22, %22githubusercontent.com%22
+domains
 ));%0A

add cert for https.