package cn.muchinfo.rma.netManage.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


import com.blankj.utilcode.util.LogUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by cxl on 17/6/6.
 * 压缩线程
 */

public class ZipThread extends Thread {

    private String imgPath;
    private String outPath;
    private double maxSize;
    private boolean needsDelete;
    private Handler handler;

    private OnZipListener mListener;

    public ZipThread(final String imgPath, final String outPath, final double maxSize, final boolean needsDelete, Handler handler) {
        this.imgPath = imgPath;
        this.outPath = outPath;
        this.maxSize = maxSize;
        this.needsDelete = needsDelete;
        this.handler = handler;
    }

    public void setListener(OnZipListener listener) {
        this.mListener = listener;
    }

    @Override
    public void run() {  //启动线层  开始进行压缩
        super.run();
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            //比例
            int options = 100;
            //将位图存储到输出流（无压缩）
            getBitmap(imgPath).compress(Bitmap.CompressFormat.JPEG, options, os);

            //按循环压缩
            while (os.toByteArray().length / 1024 > maxSize) {
                //清理os
                os.reset();
                //间隔10

                if (options <= 0) {
                    options = 10;
                } else {
                    options = options - 10;
                }
                LogUtils.eTag("ZipThread","当前压缩百分比 " + options);
                getBitmap(imgPath).compress(Bitmap.CompressFormat.JPEG, options, os);
            }

            //生成压缩图像文件
            FileOutputStream fos = new FileOutputStream(outPath);
            fos.write(os.toByteArray());
            fos.flush();
            fos.close();
            //删除原始文件
            if (needsDelete) {
                File file = new File(imgPath);
                if (file.exists()) {
                    file.delete();
                }
            }

            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从指定的图像路径获取位图
     *
     * @param imgPath
     * @返回
     */
    public Bitmap getBitmap(String imgPath) {
        //通过图像路径获取位图
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = false;
        newOpts.inPurgeable = true;
        newOpts.inInputShareable = true;
        //不要压缩
        newOpts.inSampleSize = 1;
        newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
        return BitmapFactory.decodeFile(imgPath, newOpts);
    }

    public void finish() {
        try {
            interrupt();
            this.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
