package com.wd.workoffice.util;


import android.app.Activity;
import android.os.AsyncTask;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.AuthTypeEnum;
import com.obs.services.model.HeaderResponse;
import com.obs.services.model.ObjectMetadata;
import com.obs.services.model.PostSignatureRequest;
import com.obs.services.model.PostSignatureResponse;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkApp;
import com.wd.workoffice.bean.listener.HwUploadListener;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import flexible.xd.android_base.utils.LogUtils;
import flexible.xd.android_base.utils.TimeUtils;

/**
 * Created by Flexible on 2017/1/9 0009.
 */

public class HwObsUtil {

    private volatile static HwObsUtil keeper;

    private String SK = "IhYC7S0px9FeR4lElhBoziPzUJ5DpPjUQFa9fqwG";
    private String AK = "K2DV6W6VQ7VLVGHGCT0D";
    public  static  String END_POINT = "https://obs.cn-north-4.myhuaweicloud.com";

    public  static  String IMG_DOMAIN = "http://bat-static.obs.cn-north-4.myhuaweicloud.com/";
    ObsClient obsClient = null;

    private HwObsUtil() {
        init();
    }

    public static HwObsUtil getInstance() {
        if (keeper == null) {
            synchronized (HwObsUtil.class) {
                if (keeper == null) {
                    keeper = new HwObsUtil();
                }
            }
        }
        return keeper;
    }

    private void init() {
        // 您的工程中可以只保留一个全局的ObsClient实例
        // ObsClient是线程安全的，可在并发场景下使用
        try {
            String endPoint = END_POINT;
            String ak = AK;
            String sk = SK;
            obsClient = new ObsClient(ak, sk, endPoint);
            // 创建ObsClient实例
        } catch (ObsException e) {
            Log.e("PutObject", "Response Code: " + e.getResponseCode());
            Log.e("PutObject", "Error Message: " + e.getErrorMessage());
            Log.e("PutObject", "Error Code:       " + e.getErrorCode());
            Log.e("PutObject", "Request ID:      " + e.getErrorRequestId());
            Log.e("PutObject", "Host ID:           " + e.getErrorHostId());
        }
    }

    public void uploadObject(File file, Activity activity, HwUploadListener listener) {
        String fileName = TimeUtils.getNowMills() + file.getName();
        // 调用接口进行操作，例如上传对象，其中localfile为待上传的本地文件路径，需要指定到具体的文件名
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> headers = new HashMap<String, String>();
                String contentType = "image/*";
                headers.put("Content-Type", contentType);

                PutObjectRequest request = new PutObjectRequest();
                request.setBucketName("bat-static");
                request.setObjectKey(fileName);
                request.setFile(file); // localfile为待上传的本地文件路径，需要指定到具体的文件名
                // 设置对象访问权限为公共读
                request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentType("image/png");
                request.setMetadata(metadata);
                PutObjectResult response = obsClient.putObject(request);
//                HeaderResponse response = obsClient.putObject(, fileName, file);
                LogUtils.LOGE("data", JSON.toJSONString(response, true));
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (response.getStatusCode() == 200) {
                            listener.uploadSuccess(response.getObjectKey());
                        } else {
                            listener.uploadFail(response.getObjectKey());
                        }
                    }
                });

            }
        }).start();
    }

    public void close() {
        // 关闭ObsClient实例，如果是全局ObsClient实例，可以不在每个方法调用完成后关闭
        // ObsClient在调用ObsClient.close方法关闭后不能再次使用
        if (obsClient != null) {
            try {
                obsClient.close();
            } catch (IOException e) {
            }
        }
    }

}
