package com.hainaos.vc.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.arialyy.aria.core.Aria;
import com.google.gson.JsonObject;

import java.io.File;

public class JgyUtils {
    private static final String TAG = "JGYUtils";
    @SuppressLint("StaticFieldLeak")
    private static JgyUtils sInstance;
    private Context mContext;


    private JgyUtils(Context context) {
        if (context == null) {
            throw new RuntimeException("Context is NULL");
        }
        this.mContext = context;
    }

    public static void init(Context context) {
        if (sInstance == null) {
            sInstance = new JgyUtils(context);
        }
    }

    public static JgyUtils getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException("You must be init JGYUtils first");
        }
        return sInstance;
    }

    public String getDownLoadPath() {
        String path = ContextCompat.getExternalFilesDirs(mContext, Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath();
        return path + File.separator;
    }

    public boolean fileExists(String url) {
        if (TextUtils.isEmpty(url))
            return false;
        if (VideoUtils.isUrl(url)) {
            String fileName = VideoUtils.getFileNamefromURL(url);
            if (TextUtils.isEmpty(fileName))
                return false;
            File file = new File(getDownLoadPath() + fileName);
            return file.exists();
        } else {
            File file = new File(url);
            return file.exists();
        }
    }

    public String getUrlLocalPath(String url) {
        String fileName = VideoUtils.getFileNamefromURL(url);
        return getDownLoadPath() + fileName;
    }


    public void ariaDownload(String url, JsonObject jsonObject) {
        String fileName = VideoUtils.getFileNamefromURL(url);
        String urlMd5 = "";
        if (jsonObject.get("MD5") != null) {
            urlMd5 = jsonObject.get("MD5").getAsString();
        } else if (jsonObject.get("app_md5") != null) {
            urlMd5 = jsonObject.get("app_md5").getAsString();
        } else if (jsonObject.get("file_md5") != null) {
            urlMd5 = jsonObject.get("file_md5").getAsString();
        }

        Log.e("ariaDownload", "urlMd5=" + urlMd5);
        File file = new File(JgyUtils.getInstance().getDownLoadPath() + fileName);
        if (file.exists() && !file.isDirectory()) {
            String fileMD5 = VideoUtils.getFileMD5s(file);
            Log.e("ariaDownload", "fileMD5=" + fileMD5);
            //后端还没有完成json字段的MD5，暂时用文件名获取
            if (fileMD5.equalsIgnoreCase(urlMd5)) {

            } else {
                Aria.download(mContext)
                        .load(url)     //读取下载地址
                        .setFilePath(JgyUtils.getInstance().getDownLoadPath() + fileName)
                        .setExtendField(jsonObject.toString())
                        .create();   //启动下载}
            }
        } else {
            Aria.download(mContext)
                    .load(url)     //读取下载地址
                    .setFilePath(JgyUtils.getInstance().getDownLoadPath() + fileName)
                    .setExtendField(jsonObject.toString())
                    .create();   //启动下载}
        }
    }

}
