package com.tofly.ghrq_route.module.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.incident.MonitorTool;
import com.tofly.ghrq_route.module.incident.domain.CarUploadBean;
import com.tofly.ghrq_route.module.incident.domain.DeviceUploadBean;
import com.tofly.ghrq_route.common.util.net.NetUtil;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by mgq on 2016/6/13.
 */
public class GetDeviceUploadService extends Service {

    public interface GetDeviceUpload {
        void onGetDevices(List<DeviceUploadBean> uploadBeans); // 网络状态改变之后，通过此接口的实例通知当前网络的状态，此接口在Activity中注入实例对象
    }

    public interface GetCarUpload {
        void onGetCars(List<CarUploadBean> uploadBeans); // 网络状态改变之后，通过此接口的实例通知当前网络的状态，此接口在Activity中注入实例对象
    }

    private GetDeviceUpload onGetDeviceUpload;

    private GetCarUpload onGetCarUpload;

    private String mDepartmentId;

    private boolean isCarMode = false;

    public void resetTimer() {
        timerTask = new GetDeviceUploadTimer(this);
        int period = BaseApp.getInstance().getRefreshInterval();
        mTimer.schedule(new GetDeviceUploadTimer(this), 0, period);
    }

    public void stopTimer() {
        if (timerTask != null)
            timerTask.cancel();
    }

    public void setonGetDeviceUpload(String departId, GetDeviceUpload onGetDeviceUpload) {
        isCarMode = false;
        mDepartmentId = departId;
        this.onGetDeviceUpload = onGetDeviceUpload;
    }

    public void setOnGetCarUpload(GetCarUpload onGetCarUpload) {
        isCarMode = true;
        this.onGetCarUpload = onGetCarUpload;
    }

    private Binder binder = new MyBinder();

    private Timer mTimer = null;

    private TimerTask timerTask = null;

    private MonitorTool monitorTool;

    private List<DeviceUploadBean> deviceUploadBeans;

    private List<CarUploadBean> carUploadBeans;

    @Override
    public IBinder onBind(Intent intent)
    {
        return binder;
    }

    @Override
    public void onCreate() {
        // 注册广播
        monitorTool = MonitorTool.getInstance();
        mTimer = new Timer();
        startTimer();
    }

    private void startTimer() {
        timerTask = new GetDeviceUploadTimer(this);
        int period = BaseApp.getInstance().getRefreshInterval();
        mTimer.schedule(new GetDeviceUploadTimer(this), 500, period);
    }

    class GetDeviceUploadTimer extends TimerTask {
        private Context context;

        private GetDeviceUploadTimer(Context context) {
            this.context = context;
        }

        @Override
        public void run() {
            if (NetUtil.getNetworkState(context) != NetUtil.NETWORN_NONE) {
                if (isCarMode) {
                    monitorTool.getCarUploadListOnline(context, uploadBeans -> {
                        carUploadBeans = uploadBeans;
                        if (onGetCarUpload != null) {
                            onGetCarUpload.onGetCars(carUploadBeans);
                        }
                    });
                }else {
                    monitorTool.getDeviceUploadListOnline(context, mDepartmentId, uploadBeans -> {
                        deviceUploadBeans = uploadBeans;
                        if (onGetDeviceUpload != null) {
                            onGetDeviceUpload.onGetDevices(deviceUploadBeans); // 通知网络状态改变
                        }
                    });
                }
            }
        }
    }

    public class MyBinder extends Binder {
        public GetDeviceUploadService getService()
        {
            return GetDeviceUploadService.this;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
        mTimer = null;
    }
}