package com.videogo.ui.playback.querylist;

import com.videogo.EzvizApplication;
import com.videogo.exception.BaseException;
import com.videogo.openapi.bean.EZCloudRecordFile;
import com.videogo.openapi.bean.resp.CloudPartInfoFile;
import com.videogo.ui.playback.RemoteListContant;
import com.videogo.ui.common.HikAsyncTask;
import com.videogo.util.CollectionUtil;
import com.videogo.util.DateTimeUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class QueryCloudRecordFilesAsyncTask extends HikAsyncTask<String, Void, Integer> {

    // 设备序列号
    private String deviceSerial;
    // 通道号
    private int channelNo;
    // 搜索日期
    private Date searchDate;
    private volatile boolean abort = false;
    private QueryPlayBackListTaskCallback queryPlayBackListTaskCallback;
    private List<CloudPartInfoFile> cloudPartFiles;

    public QueryCloudRecordFilesAsyncTask(String deviceSerial, int channelNo,
                                          QueryPlayBackListTaskCallback queryPlayBackListTaskCallback) {
        this.deviceSerial = deviceSerial;
        this.channelNo = channelNo;
        this.queryPlayBackListTaskCallback = queryPlayBackListTaskCallback;
    }

    public void setAbort(boolean abort) {
        this.abort = abort;
    }

    public void setSearchDate(Date searchDate) {
        this.searchDate = searchDate;
    }

    @Override
    protected Integer doInBackground(String... params) {
        return queryCloudFile();
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (!abort) {
            if (result == RemoteListContant.QUERY_NO_DATA) {
                queryPlayBackListTaskCallback.queryHasNoData(RemoteListContant.TYPE_CLOUD);
            } else if (result == RemoteListContant.QUERY_CLOUD_SUCCESSFUL) {
                queryPlayBackListTaskCallback.querySuccessFromCloud(cloudPartFiles);
            }
        }
    }

    private void convertEZCloudRecordFile2CloudPartInfoFile(CloudPartInfoFile dst, EZCloudRecordFile src, int pos) {
        String startT = DateTimeUtil.calendarToYMDHMSCompactString(src.getStartTime());
        String endT = DateTimeUtil.calendarToYMDHMSCompactString(src.getStopTime());
        dst.setCloud(true);
        dst.setDownloadPath(src.getDownloadPath());
        dst.setEndTime(endT);
        dst.setFileId(src.getFileId());
        dst.setKeyCheckSum(src.getEncryption());
        dst.setPicUrl(src.getCoverPic());
        dst.setPosition(pos);
        dst.setStartTime(startT);
        dst.setDeviceSerial(src.getDeviceSerial());
        dst.setCameraNo(src.getCameraNo());
        dst.setVideoType(src.getVideoType());
        dst.setCloudType(src.getCloudType());
        dst.setiStorageVersion(src.getiStorageVersion());
        dst.setFileSize(src.getFileSize());
    }

    private int queryCloudFile() {
        Date beginDate = DateTimeUtil.beginDate(searchDate);
        Date endDate = DateTimeUtil.endDate(searchDate);
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(beginDate);
        endTime.setTime(endDate);

        List<EZCloudRecordFile> tmpList = null;
        try {
            tmpList = EzvizApplication.getOpenSDK().searchRecordFileFromCloud(deviceSerial, channelNo,
                    startTime, endTime);
        } catch (BaseException e) {
            e.printStackTrace();
        }

        cloudPartFiles = new ArrayList<>();
        if (tmpList != null && tmpList.size() > 0) {
            for (int i = 0; i < tmpList.size(); i++) {
                EZCloudRecordFile file = tmpList.get(i);
                CloudPartInfoFile cpif = new CloudPartInfoFile();
                convertEZCloudRecordFile2CloudPartInfoFile(cpif, file, i);
                cloudPartFiles.add(cpif);
            }
        }

        if (CollectionUtil.isNotEmpty(cloudPartFiles)) {
            Collections.sort(cloudPartFiles);
            Collections.reverse(cloudPartFiles);
            // 排序后重新设置position
            for (int i = 0; i < cloudPartFiles.size(); i++) {
                cloudPartFiles.get(i).setPosition(i);
            }
            return RemoteListContant.QUERY_CLOUD_SUCCESSFUL;
        }
        return RemoteListContant.QUERY_NO_DATA;
    }

}
