package com.fxy.utils;

import android.content.Context;

import com.fxy.baselibrary.language.other.CommSharedUtil;
import com.fxy.realm.AsyncUploadRealm;

import java.util.Calendar;

import io.realm.Realm;
import io.realm.RealmResults;

public class UploadUtils {
    public static String currentUploadTimeTag="oss_current_upload_time";
    public static String interfaceRequestTimeTag="interface_request_time";
     public static int getAwaitNum(Realm mRealm,String uploadType){
        RealmResults<AsyncUploadRealm> realmResults  = mRealm.where(AsyncUploadRealm.class)
                .equalTo("isDelete",0)
                .equalTo("pushStatus",0)
                .equalTo("uploadType",uploadType)
                .sort("pushStatus").findAll();
        realmResults.load();
        return  realmResults.size();
    }
    public static int getAwaitNum(Realm mRealm, String[] uploadTypeList){
        RealmResults<AsyncUploadRealm> realmResults  = mRealm.where(AsyncUploadRealm.class)
                .equalTo("isDelete",0)
                .equalTo("pushStatus",0)
                .in("uploadType",uploadTypeList)
                .sort("pushStatus").findAll();
        realmResults.load();
        return  realmResults.size();
    }
    public static long getCurrentTime(){
        Calendar calendar = Calendar.getInstance();

        return calendar.getTimeInMillis();
    }

    /**
     * 计算两个时间戳之间的差异，并返回以秒为单位的结果。
     *
     * @param timestamp1 第一个时间戳（毫秒）
     * @param timestamp2 第二个时间戳（毫秒）
     * @return 差异的秒数
     */
    public static long calculateDifferenceInSeconds(long timestamp1, long timestamp2) {
        // 计算两个时间戳之间的差异（毫秒）
        long differenceInMillis = Math.abs(timestamp1 - timestamp2);
        // 将差异转换为秒
        return differenceInMillis / 1000;
    }

    /**
     * 获取是否允许上传
     * @param context Context
     * @return boolean
     */
    public static boolean getUploadExists(Context context){
       String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(currentUploadTimeTag);

       if (currentUploadTimeStr==null ||currentUploadTimeStr.isEmpty()){
           return true;
       }
        long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.parseLong(currentUploadTimeStr));
        return diff > 60 * 3;
    }

    /**
     * 设置上传时间
     * @param context Context
     */
    public static void setUploadTime(Context context){
        CommSharedUtil.getInstance(context).putString(currentUploadTimeTag,String.valueOf(getCurrentTime()));
    }

    /**
     * 删除上传时间
     * @param context Context
     */
    public static void delUploadTime(Context context){
        CommSharedUtil.getInstance(context).remove(currentUploadTimeTag);
    }

    /**
     * 获取是否继续
     * @param context Context
     * @return boolean
     */
    public static boolean getUploadIsContinue(Context context){
        String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(interfaceRequestTimeTag);

        if (currentUploadTimeStr==null ||currentUploadTimeStr.isEmpty()){
            return true;
        }
        long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.parseLong(currentUploadTimeStr));
        //
        return diff > 60 * 5;
    }

    /**
     * 设置上传时间
     * @param context Context
     */
    public static void setInterfaceRequestTime(Context context){
        CommSharedUtil.getInstance(context).putString(interfaceRequestTimeTag,String.valueOf(getCurrentTime()));

    }
    /**
     * 删除上传时间
     * @param context Context
     */
    public static void delInterfaceRequestTime(Context context){
        CommSharedUtil.getInstance(context).remove(interfaceRequestTimeTag);
    }
}
