package com.sheep.gamegroup.model.util;

import android.support.v7.app.AlertDialog;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.sheep.gamegroup.model.entity.AppRecord;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.gamegroup.model.entity.TaskReleaseEty;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by realicing on 2018/6/15.
 * realicing@sina.com
 */
public class AutoTaskListUtil {

    private static AutoTaskListUtil autoTaskListUtil = new AutoTaskListUtil();

    private AutoTaskListUtil(){
        resetReceiveTaskTime();
    }
    public static AutoTaskListUtil getInstance(){
        if (autoTaskListUtil == null){
            autoTaskListUtil = new AutoTaskListUtil();
        }
        return autoTaskListUtil;
    }
    private Map<String, TaskEty> map = new HashMap<>();
    public boolean addTask(TaskEty task) {
        if(map == null)
            map = new HashMap<>();
        if(task != null && !TextUtils.isEmpty(task.getPackage_names())){
            if(task.isAppAutoCommitTask()) {
                map.put(task.getPackage_names(), task);
                return true;
            }
        }
        return false;
    }
    public boolean addTask(TaskReleaseEty taskReleaseEty) {
        if(taskReleaseEty != null && taskReleaseEty.getTask() != null && taskReleaseEty.getTask().isAppAutoCommitTask()) {
            taskReleaseEty.getTask().setRelease_task_id(taskReleaseEty.getId());
            return addTask(taskReleaseEty.getTask());
        }
        return false;
    }

    public boolean isEmpty() {
        return map == null || map.isEmpty();
    }
    private boolean loadding = false;
    public void initTaskList() {
        if(loadding){
            return;
        }
        loadding = true;
        //下面的功能主要是初始化 正在运行的应用自动审核任务列表
        AutoTaskListUtil.getInstance().clear();
        SheepApp.getInstance().getNetComponent().getApiService()
                .returnTask(3, 1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.mContext) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        loadding = false;
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<TaskAcceptedEty> list = baseMessage.getDatas(TaskAcceptedEty.class);
                        loadList(list);
                        loadding = false;
                    }
                });
    }

    public void loadList(List<TaskAcceptedEty> list) {
//        DataUtil.getInstance().addTaskAcceptedEtyList(list);//添加正在运行的任务列表到内存中
        clear();
        if(list != null) {
            for (TaskAcceptedEty item : list) {
                if (item != null && item.isIs_running())
                   addTask(item.getRelease_task());
            }
        }
    }

    public void clear() {
        if(map != null)
            map.clear();
    }

    public boolean hasTask(String packageName) {
        return !TextUtils.isEmpty(packageName) && map != null && map.containsKey(packageName);
    }

    public boolean hasTask(AppRecord appRecord) {
        return appRecord != null && hasTask(appRecord.getPackageName());
    }




    private AppRecord lastAppRecord;
    private long lastTime;
    public AppRecord getLastAppRecord() {
        return lastAppRecord;
    }

    public long getLastTime() {
        return lastTime;
    }

    public void setLastAppRecord(AppRecord lastAppRecord) {
        this.lastAppRecord = lastAppRecord;
    }

    public void setLastTime(long lastTime) {
        this.lastTime = lastTime;
    }





    private AlertDialog tipToOpernAccessibilityDialog;
    public void clearAlertDialog(){
        tipToOpernAccessibilityDialog = null;
    }

    public AlertDialog getTipToOpernAccessibilityDialog() {
        return tipToOpernAccessibilityDialog;
    }

    public void setTipToOpernAccessibilityDialog(AlertDialog tipToOpernAccessibilityDialog) {
        this.tipToOpernAccessibilityDialog = tipToOpernAccessibilityDialog;
    }

    public boolean isShowing() {
        return tipToOpernAccessibilityDialog != null && tipToOpernAccessibilityDialog.isShowing();
    }
    //正在运行的app没有变化，即传入的包名与lastAppRecord中的包名相同
    public boolean isLastAppRecord(String packageName) {
        return lastAppRecord != null && TextUtils.equals(packageName, lastAppRecord.getPackageName());
    }




    private long receiveTaskTime;//使用UsageStatsManager来计算应用使用时长时的开始计算时间
    /**
     * 重置比对时间  5.1及以上使用
     */
    public void resetReceiveTaskTime(){//点击领取应用自动审核时,重置这个时间；提交领取奖励时也要重置这个时间
        receiveTaskTime = System.currentTimeMillis();
    }

    public long getReceiveTaskTime() {
        return receiveTaskTime;
    }
}
