package com.xiaoyudesign.rnupdate;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;

import java.io.File;

/**
 * 下载参数
 */
public class UpdateDownloadParams {

    /**
     * 下载的路径
     */
    String      url;
    /**
     * 输出地址的文件
     */
    File        outputFile;

    /**
     * 连接http超时时间 （ms）
     */
    int         timeout = 30 * 1000;

    /**
     * 读取包超时时间 （ms）
     */
    int         readTimeout = 60 * 1000;

    /**
     * 下载进度回调
     */
    DownloadProgressCallback downloadProgressCallback;

    public UpdateDownloadParams(String url, File outputFile, DownloadProgressCallback progressCallback) {
        this.url = url;
        this.outputFile = outputFile;
        this.downloadProgressCallback = progressCallback;
    }

    public interface DownloadProgressCallback {
        /**
         * 下载进度回调
         * @param currentSize 已下载字节数
         * @param totalSize 总字节数（若无法获取总大小，返回 -1）
         * @param progress 下载进度（0-100，若无法计算，返回 -1）
         */
        void onDownloadProgress(long currentSize, long totalSize, int progress);

        /**
         * 可选：下载状态回调（如开始、暂停、失败，按需添加）
         * @param status 下载状态（自定义枚举，更易维护）
         * @param errorMsg 错误信息（失败时返回，成功/开始时为 null）
         */
        default void onDownloadStatus(DownloadStatus status, String errorMsg) {}

        /**
         * （可选）下载完成的回调，也可以在 onDownloadStatus中获取，是一样的，这个只是提出来了
         */
        default void onDownloadComplete() {}

    }

    public enum DownloadStatus {
        DOWNLOAD_START(1001, "下载开始"),
        DOWNLOADING(1002, "下载中"),
        DOWNLOAD_SUCCESS(1003, "下载成功"),
        DOWNLOAD_FAILED(1004, "下载失败"),
        UNZIP_FAILED(1005, "解压缩失败"),
        DOWNLOAD_PARAMS_NOT_EMPTY(1500, "下载参数不能为空");

        // 自定义字段：枚举对应的固定数值和描述
        public final int code;
        public String desc;

        DownloadStatus(int code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public DownloadStatus setDesc(String desc) {
            this.desc = desc;
            return this;
        }

        public ReadableMap toReadableMap() {
            WritableMap map = Arguments.createMap();
            map.putInt("code", this.code);
            map.putString("msg", this.desc);
            return map;
        }
    }
}
