package com.videogo.ui.playback.querylist;

import com.videogo.EzvizApplication;
import com.videogo.errorlayer.ErrorInfo;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.bean.EZDeviceRecordFile;
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 com.videogo.util.LogUtil;
import com.videogo.util.Utils;

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

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

    // 通道号
    private int channelNo;
    // 设备序列号
    private String deviceSerial;
    // 录像类型
    private EZConstants.EZVideoRecordType recordType;
    // 查询日期
    private Date queryDate;
    private List<CloudPartInfoFile> convertCalendarFiles;
    private QueryPlayBackListTaskCallback playBackListTaskCallback = null;
    private volatile boolean abort = false;

    public QueryDeviceRecordFilesAsyncTask(String deviceSerial, int channelNo, EZConstants.EZVideoRecordType recordType,
                                           QueryPlayBackListTaskCallback playBackListTaskCallback) {
        this.channelNo = channelNo;
        this.deviceSerial = deviceSerial;
        this.recordType = recordType;
        this.playBackListTaskCallback = playBackListTaskCallback;
    }

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

    public void setQueryDate(Date queryDate) {
        this.queryDate = queryDate;
    }

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

    @Override
    protected void onPostExecute(Integer result) {
        if (!abort) {
            if (result == RemoteListContant.QUERY_LOCAL_SUCCESSFUL) {
                playBackListTaskCallback.querySuccessFromDevice(convertCalendarFiles);
            } else if (result == RemoteListContant.QUERY_NO_DATA) {
                playBackListTaskCallback.queryHasNoData(RemoteListContant.TYPE_LOCAL);
            }
        }
    }

    private void convertEZDeviceRecordFile2CloudPartInfoFile(CloudPartInfoFile dst, EZDeviceRecordFile src, int pos) {
        dst.setStartTime(DateTimeUtil.calendarToYMDHMSCompactString(src.getStartTime()));
        dst.setEndTime(DateTimeUtil.calendarToYMDHMSCompactString(src.getStopTime()));
        dst.setPosition(pos);
        dst.setBegin(src.getBegin());
        dst.setEnd(src.getEnd());
    }

    public int searchLocalFile() {
        Date beginDate = DateTimeUtil.beginDate(queryDate);
        Date endDate = DateTimeUtil.endDate(queryDate);
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(beginDate);
        endTime.setTime(endDate);

        List<EZDeviceRecordFile> tmpList = null;
        try {
            tmpList = EzvizApplication.getOpenSDK().searchRecordFileFromDevice(deviceSerial, channelNo, startTime, endTime, recordType);
        } catch (BaseException e) {
            e.printStackTrace();
            ErrorInfo errorInfo = e.getObject();
            LogUtil.d("search file list failed. error ", errorInfo.toString());
        }

        convertCalendarFiles = new ArrayList<>();
        if (tmpList != null && tmpList.size() > 0) {
            for (int i = 0; i < tmpList.size(); i++) {
                EZDeviceRecordFile file = tmpList.get(i);
                CloudPartInfoFile cpif = new CloudPartInfoFile();
                // 云存储和SD卡录像共用组件，所以需要做对象转换
                convertEZDeviceRecordFile2CloudPartInfoFile(cpif, file, i);
                convertCalendarFiles.add(cpif);
            }
        }

        if (CollectionUtil.isNotEmpty(convertCalendarFiles)) {
            Collections.sort(convertCalendarFiles);
            Collections.reverse(convertCalendarFiles);
            // 排序后重新设置position
            for (int i = 0; i < convertCalendarFiles.size(); i++) {
                convertCalendarFiles.get(i).setPosition(i);
            }
            // 修正跨天的时间
            for (CloudPartInfoFile file : convertCalendarFiles) {
                Calendar endCal = Utils.convert14Calender(file.getEndTime());
                if (endCal.getTimeInMillis() > endDate.getTime()) {
                    Calendar fixedEnd = Calendar.getInstance();
                    fixedEnd.setTime(endDate);
                    file.setEndTime(DateTimeUtil.calendarToYMDHMSCompactString(fixedEnd));
                }
            }
            return RemoteListContant.QUERY_LOCAL_SUCCESSFUL;
        }
        return RemoteListContant.QUERY_NO_DATA;
    }

}
