package com.qimai.log;

import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.TreeMap;

import androidx.annotation.Nullable;

/**
 * Created by wanglei on 18-7-16.
 * <p>
 * 根据通知去上传日志
 */

public class UploadService extends IntentService {


    public UploadService() {
        super("upload");


    }

    private static final String TAG = "UploadService";

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public UploadService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        Log.d(TAG, "onHandleIntent: ");
        // updateOldOrder();
        try {
            UploadConfig receiverBean = (UploadConfig) intent.getSerializableExtra("upload");

            if (receiverBean != null) {
                uploadLog(receiverBean);
            }
        } catch (Exception e) {


        }

    }

    private void uploadLog(UploadConfig config) {
        //DBCopyUtils.copyLogToFTP();
        //int requestNum = receiverBean.getNum();
        File file = null;
        // final String logPath = SDCARD + "/ptcy/log";
        final String logPath;
        if (BuildConfig.DEBUG) {

            logPath = QmLog.INSTANCE.getConfig().getLogDebugPath();
        } else {
            logPath = QmLog.INSTANCE.getConfig().getLogReleasePath();

        }
        file = new File(logPath);
        if (!file.exists()) {
            return;
        }
        //file.lastModified();
        File[] files = convertTimeDesc(file.listFiles());
        if (files == null || files.length == 0) {
            return;
        }
        // int maxFile = files.length;
        // int endNum = Math.min(maxFile,num);
        for (int i = 0; i < files.length; i++) {
            File tempFile = files[i];
            DBCopyUtils.copyLogToFTP(tempFile,config.getTag());
        }
    }

    private File[] convertTimeDesc(File[] files) {

        if (files == null || files.length == 0) {

            return null;
        }

        int endNum = files.length;
        File[] filesAll = new File[20];
        TreeMap<Long, File> longFileTreeMap = new TreeMap<>();

        for (int i = 0; i < files.length; i++) {

            Long temp = files[i].lastModified();
            if (files[i].exists()) {
                longFileTreeMap.put(temp, files[i]);
            }


        }

        NavigableSet<Long> navigableSet = longFileTreeMap.descendingKeySet();

        Iterator<Long> iterator = navigableSet.iterator();

        int index = 0;
        while (iterator.hasNext()) {

            File file = longFileTreeMap.get(iterator.next());
            Log.d(TAG, "convertTimeDesc: file name= " + file.getName());
            filesAll[index] = file;
            index++;
            if (index >= endNum) {

                break;
            }
        }

        return filesAll;
    }


}
