package com.sunzee.mvp.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.sunzee.R;
import com.sunzee.base.BaseApplication;
import com.sunzee.base.BasePresenter;
import com.sunzee.model.Heartbeat;
import com.sunzee.model.HttpResult;
import com.sunzee.model.domain.ApkInfoBean;
import com.sunzee.model.domain.Name;
import com.sunzee.retrofit.ApiCallback;
import com.sunzee.service.DownServer;
import com.sunzee.utils.FileUtil;
import com.sunzee.utils.SharedPreferencesUtils;
import com.sunzee.utils.ToastUtil;
import com.sunzee.utils.UiUtil;

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

/**
 * 后台首页 presenter
 */
public class HomePresenter extends BasePresenter<HomeView> {
    private static final String TAG = "HomePresenter";

    public HomePresenter(HomeView view) {
        attachView(view);
    }


    public void connectionSystem(String text) {

        SharedPreferencesUtils.setParam(Name.CONNECT_STATE, 1);
        mvpView.showLoading();
        if (Heartbeat.clientId == null || Heartbeat.clientId.equals("")) {
            ToastUtil.showToast(UiUtil.getStringRes(R.string.idsb));
            return;
        }

        SharedPreferencesUtils.setParam(Name.CONNECT_STATE, 2);
        Heartbeat.deviceId = FileUtil.getDeviceId();
        SharedPreferencesUtils.setParam(Name.CONNECT_STATE, -1);
        Map<String, String> params = new HashMap<>();
        params.put("clientId", Heartbeat.deviceId);
        params.put("managerId", text);
        params.put("gtClientId", Heartbeat.clientId);
        Heartbeat.managerId = text;
        addSubscription(apiStores.init(params), new ApiCallback<HttpResult<String>>() {
            @Override
            public void onSuccess(HttpResult<String> model) {
                Log.d(TAG, "onSuccess: " + model);
                if (model.getData()==null) {
                    mvpView.initSuccess(model.getErrorMsg());
                }else if (model.getErrorMsg()==null){
                    mvpView.initSuccess(model.getData());
                }
            }

            @Override
            public void onFailure(String msg) {
                Log.d(TAG, "onFailure: " + msg);
            }

            @Override
            public void onFinish() {
                mvpView.hideLoading();
            }
        });
    }

    /**
     * 获取下载连接
     */
    public void updataapk() {
        Map<String, String> map = new HashMap<>();
        map.put("model", "301ch");
        addSubscription(apiStores.updataAPK(map), new ApiCallback<HttpResult<String>>() {
            @Override
            public void onSuccess(HttpResult<String> model) {
                Log.d(TAG, "onFailure: "+model);
                Gson gson=new Gson();
                ApkInfoBean apkInfoBean = gson.fromJson(model.getData(), ApkInfoBean.class);
                mvpView.updataSuccess(apkInfoBean);
            }

            @Override
            public void onFailure(String msg) {
                Log.d(TAG, "onFailure: "+msg);
            }

            @Override
            public void onFinish() {

            }
        });
    }

    /**
     * 下载apk
     *
     * @param url     地址
     * @param version 版本号（用于起名）
     */
    public void downloadApk(String url, int version) {
        //开始下载
        Intent downloadApkIntent = new Intent(BaseApplication.getContext(), DownServer.class);
        Bundle bundle = new Bundle();
        bundle.putString("downloadApkUrl", url);
        bundle.putString("title", "mht" + ".apk");
        bundle.putString("downloadtype", "apk");
        downloadApkIntent.putExtra("download", bundle);
        BaseApplication.getContext().startService(downloadApkIntent);
    }
}
