package com.sunzee.mvp.homepage;

import android.util.Log;

import com.hboxs.serialport.util.HexUtils;
import com.orhanobut.hawk.Hawk;
import com.sunzee.base.BasePresenter;
import com.sunzee.model.Heartbeat;
import com.sunzee.model.HttpResult;
import com.sunzee.model.domain.Name;
import com.sunzee.retrofit.ApiCallback;
import com.sunzee.thread.homepage.ThreadPoolHomepage;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 首页界面 presenter
 */
public class HomePagePresenter extends BasePresenter<HomePageView> {
    private static final String TAG = "HomePagePresenter";

    public HomePagePresenter(HomePageView view) {
        attachView(view);
    }

    /**
     * 是否可以清洗
     *
     * @return
     */
    public void isCanWrite() {
        isCleaned();
    }

    private void isCleaned() {
        addSubscription(apiStores.getBackgroundTime(), new ApiCallback<HttpResult<String>>() {
            @Override
            public void onSuccess(HttpResult<String> model) {
                try {
                    String timedata = model.getData();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timedata));
                    //第一次就会出现报错。
                    Long overdueDate = (Long) Hawk.get(Name.OVERDUE_DATE);//购买的到期的时间
                    Long timeInMillis = calendar.getTimeInMillis();//当前时间
                    String data = (String) Hawk.get(Name.REMAINING);
                    long remaining = Long.valueOf(data);
                    Log.d(TAG, "onSuccess: 时间过期remaining" + remaining);
                    //如果时间没过期，那么可以继续制作
                    if (overdueDate > timeInMillis) {
                        Log.d(TAG, "onSuccess: 没有过期");
                        //发送开始清洗的订单。
                        getCleanReport(1);
                        //如果还有次那么就开始购买。
                    } else if (remaining > 0) {
                        Log.d(TAG, "onSuccess: 时间过期，按次数计算");
                        Hawk.put(Name.REMAINING, String.valueOf(remaining - 1));
                        //发送开始清洗的订单。
                        getCleanReport(1);
                    } else {
                        Log.d(TAG, "onSuccess: 全部过期，全部不能清洗");
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onFinish() {

            }
        });

    }

    private void getCleanReport(int type) {
        Map<String, String> params = new HashMap<>();
        params.put("clientId", Heartbeat.deviceId);
        params.put("type", type + "");
        params.put("water", 1 + "");
        params.put("electricity", 1 + "");
        addSubscription(apiStores.getCleanReport(params), new ApiCallback<HttpResult<String>>() {

            @Override
            public void onSuccess(HttpResult<String> model) {
                if ("success".equals(model.getData())) {
                    mvpView.startClean();
                } else {
                    mvpView.haveExpired();
                }
            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onFinish() {

            }
        });
    }
}
