package com.app.duck.util;

import static com.app.duck.Config.BREATH_TAG;
import static com.blankj.utilcode.util.TimeUtils.getSafeDateFormat;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.app.duck.Config;
import com.app.http.APPConfig;
import com.app.http.BaseBean;
import com.app.http.Http;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.elvishew.xlog.XLog;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class HeartbeatWorker extends Worker {
    public HeartbeatWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        if (StringUtils.isEmpty(HxUtils.getPhone())) {
            LiveEventBus.get(BREATH_TAG).post("duck.1 retrying.[No phone number set yet.]");
            Log.i(Config.LOG_TAG, "duck.1 retrying.[No phone number set yet.]");
            XLog.i("duck.1 retrying.[No phone number set yet.]");
            return Result.retry();
        }
        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> mapItem = new HashMap<>();
        mapItem.put("address", HxUtils.getPhone());
        mapItem.put("status", "success");
        map.put("data", GsonUtils.toJson(mapItem));
        map.put("password", "o6M5sG7E@FAWLBL9");
        Http.getInstance()
                .setUrlPath(APPConfig.BASE, APPConfig.SMS)
                .setParams(map)
                .setErrorStyle(Http.ERROR_HIDE)
                .setLoadStyle(Http.ERROR_HIDE)
                .setRetryCount(10)
                .post(new Http.HttpCallBack<String>() {
                    @Override
                    public void onNext(String model) {
                        LiveEventBus.get(BREATH_TAG).post("duck.1 connected." + TimeUtils.getNowString(getSafeDateFormat("MM-dd HH:mm")));
                        Log.i(Config.LOG_TAG, "duck.1 connected.");
                        XLog.i("duck.1 connected.");
                    }

                    @Override
                    public void onError(Throwable ex) {
                        super.onError(ex);
                        Log.e(Config.LOG_TAG, "duck.1 disconnected.", ex);
                        XLog.e("duck.1 disconnected.", ex);
                        LiveEventBus.get(BREATH_TAG).post("duck.1 disconnected." + TimeUtils.getNowString(getSafeDateFormat("MM-dd HH:mm")));
                    }

                    @Override
                    public void onFail(BaseBean t) {
                        super.onFail(t);
                        Log.i(Config.LOG_TAG, "duck.1 disconnected.[" + t.getMsg() + "]");
                        XLog.i("duck.1 disconnected.[" + t.getMsg() + "]");
                        LiveEventBus.get(BREATH_TAG).post("duck.1 disconnected.[" + t.getMsg() + "]" + TimeUtils.getNowString(getSafeDateFormat("MM-dd HH:mm")));

                    }
                });
        return Result.success();
    }

    public static void start() {
        XLog.i("duck.1 start.");
        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(HeartbeatWorker.class, 15, TimeUnit.SECONDS)
                .build();
        WorkManager.getInstance().enqueueUniquePeriodicWork("heardbeatWork", ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest);
    }

}
