package com.bbitcn.silk.ui.activity;

import android.app.Application;
import android.os.Build;
import android.os.StrictMode;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.bbitcn.silk.base.BasePresenter;
import com.bbitcn.silk.model.net.CommonResponse;
import com.bbitcn.silk.model.net.TodayPrice;
import com.bbitcn.silk.model.net.Update;
import com.bbitcn.silk.model.net.UserInfo;
import com.bbitcn.silk.model.net.Weather;
import com.bbitcn.silk.utils.DialogUtil;
import com.bbitcn.silk.utils.MMKVUtil;
import com.bbitcn.silk.utils.database.PriceDataBase;
import com.bbitcn.silk.utils.global.Global;
import com.bbitcn.silk.utils.log.MyLog;
import com.bbitcn.silk.utils.network.Url;
import com.bbitcn.silk.utils.network.XHttpManager;
import com.bbitcn.silk.utils.network.XHttpShorthand;
import com.blankj.utilcode.util.StringUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * @Author DuanKaiji
 * @CreateTime 2024年03月27日 13:51
 */
public class MainActivityPresenter extends BasePresenter {

    private MutableLiveData<Update.DataDTO> noticeUpdate = new MutableLiveData<>();
    private MutableLiveData<UserInfo> noticeUserInfo = new MutableLiveData<>();
    private MutableLiveData<TodayPrice> noticeTodayPrice = new MutableLiveData<>();
    private MutableLiveData<Weather.DataDTO> noticeWeather = new MutableLiveData<>();

    public MutableLiveData<Update.DataDTO> getNoticeUpdate() {
        return noticeUpdate;
    }

    public MutableLiveData<Weather.DataDTO> getNoticeWeather() {
        return noticeWeather;
    }

    public MutableLiveData<TodayPrice> getNoticeTodayPrice() {
        return noticeTodayPrice;
    }

    public MutableLiveData<UserInfo> getNoticeUserInfo() {
        return noticeUserInfo;
    }

    public MainActivityPresenter(@NonNull Application application) {
        super(application);
        checkVersion();
    }

    /**
     * 检查是否需要升级APP，以及进行升级
     */
    public void checkVersion() {
        showLoadingDialog("正在检测更新中");
        if (Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.getNewVersion, false);
        x.http().get(params, new XHttpShorthand<Update>() {
            @Override
            public void success(Update model) {
                if (model.getData() != null) {
                    noticeUpdate.setValue(model.getData());
                } else if (!StringUtils.isEmpty(model.getMsg())) {
                    errorTips(model.getMsg());
                }
            }

            @Override
            public void onSuccessError(CommonResponse data) {
                successTips(data.getMsg());
            }

            @Override
            public void error(Throwable e) {
                errorTips(e.getMessage());
            }

            @Override
            public void myFinish() {
                super.myFinish();
                DialogUtil.hideLoadingDialog();
            }
        });
    }

    /**
     * 获取用户信息
     */
    public void getRefreshUserInfo() {
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.getUserInfo);
        x.http().get(params, new XHttpShorthand<UserInfo>() {
            @Override
            public void success(UserInfo model) {
                MyLog.app("用户信息获取成功");
                noticeUserInfo.setValue(model);
                MMKVUtil.put(Global.USER_NAME, model.getData().getUserName());
                MMKVUtil.put(Global.USER_ID, model.getData().getUserSysID());
                MMKVUtil.put(Global.COMPANY_NAME, model.getData().getTenantName());
                MMKVUtil.put(Global.STATION_NAME, model.getData().getDepName());
                MMKVUtil.put(Global.DEP_SYS_ID, model.getData().getDepSysid());
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("用户信息获取失败");
            }

            @Override
            public void myFinish() {
                super.myFinish();
                hideLoadingDialog();
            }
        });
    }

    /**
     * 获取今日价格
     */
    public void getPriceOfToday() {
        showLoadingDialog("正在获取今日价格");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.getPriceOfToday);
        x.http().get(params, new XHttpShorthand<TodayPrice>() {
            @Override
            public void success(TodayPrice model) {
                MyLog.app("今日价格获取成功");
                PriceDataBase.updateCatch(model.getData().getItems());
                noticeTodayPrice.setValue(model);
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("今日价格获取失败");
            }

            @Override
            public void myFinish() {
                super.myFinish();
                hideLoadingDialog();
            }
        });
    }

    /**
     * 获取天气
     */
    public void getWeather() {
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.weather);
        x.http().get(params, new XHttpShorthand<Weather>() {
            @Override
            public void success(Weather model) {
                noticeWeather.setValue(model.getData());
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("今日价格获取失败");
            }

            @Override
            public void myFinish() {
                super.myFinish();
                hideLoadingDialog();
            }
        });
    }


}
