package com.tjzhxx.union.public_store;

import android.os.Handler;
import android.os.Message;

import com.autonavi.base.amap.mapcore.FileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 描述:
 *
 * @author yhx
 * @create 2019/5/28  4:12 PM
 */
public class DownloadThread extends Thread {
    private String url;
    private String path;
    private String fileName;
    private Handler mHandler;
    private int threadId;
    private int progress;
    private boolean canceled = false;
    private int lastRate = 0;
    private String fileId;
    /**
     * 构造方法
     *
     * @param url      下载文件的地址
     * @param path     保存文件的路径
     * @param threadId 下载文件的线程
     */
    public DownloadThread(String url, String path, String fileName, Handler handler, int threadId) {
        this.url = url;
        this.path = path;
        this.threadId = threadId;
        this.fileName = fileName;
        mHandler = handler;
    }


    @Override
    public void run() {
        try {
            URL url1 = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
            conn.setRequestProperty("Accept-Encoding", "identity");
            conn.connect();
            int length = conn.getContentLength();
            conn.getHeaderField("Content-Length");

            InputStream is = conn.getInputStream();
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            File file1 = new File(path+"/"+fileName);
            FileOutputStream fos = new FileOutputStream(file1);

            int count = 0;
            byte buf[] = new byte[1024];
            do {
                int numread = is.read(buf);
                count += numread;
                progress = (int) (((float) count / length) * 100);
                // 更新进度
                Message msg = mHandler.obtainMessage();
                msg.what = 1;
                msg.arg1 = progress;
                msg.arg2 = threadId;
                msg.obj = file1;
                if (progress >= lastRate + 1) {
                    mHandler.sendMessage(msg);
                    lastRate = progress;
                }
                if (numread <= 0) {
                    // 下载完成通知安装
                    mHandler.sendEmptyMessage(0);
                    // 下载完了，cancelled也要设置
                    canceled = true;
                    break;
                }
                fos.write(buf, 0, numread);
            } while (!canceled);// 点击取消就停止下载.
            fos.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Message msg = mHandler.obtainMessage();
            msg.what = -1;
            msg.arg2 = threadId;
            mHandler.sendMessage(msg);
            FileUtil.deleteFile(new File(path+"/"+fileName));
        } catch (IOException e) {
            e.printStackTrace();
            FileUtil.deleteFile(new File(path+"/"+fileName));
            Message msg = mHandler.obtainMessage();
            msg.what = -1;
            msg.arg2 = threadId;
            mHandler.sendMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
            FileUtil.deleteFile(new File(path+"/"+fileName));
            Message msg = mHandler.obtainMessage();
            msg.what = -1;
            msg.arg2 = threadId;
            mHandler.sendMessage(msg);
        }

    }
}
