package com.phlx.anchorcollect_p.ui.feedback;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.google.gson.Gson;
import com.phlx.anchorcollect_p.Configs;
import com.phlx.anchorcollect_p.R;
import com.phlx.anchorcollect_p.data.Repository;
import com.phlx.anchorcollect_p.data.VMFactory;
import com.phlx.anchorcollect_p.entity.FeedbackEntity;
import com.phlx.anchorcollect_p.entity.LoginData;
import com.phlx.anchorcollect_p.entity.UserRoleEntity;
import com.phlx.anchorcollect_p.params.CollectResponse;
import com.phlx.anchorcollect_p.ui.base.BackBarVM;
import com.phlx.anchorcollect_p.ui.splash.SplashActivity;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 意见反馈vm
 */
public class FeedbackVM extends BackBarVM<Repository> {

    public ObservableField<String> feedContent = new ObservableField<>("");

    public FeedbackVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public void initToolbar() {
        setBackTitleText("意见反馈");
        setBackRightTextVisible(View.VISIBLE);
        setBackRightText(getApplication().getResources().getString(R.string.submit));
    }

    @Override
    protected void backRightTextOnClick() {
        super.backRightTextOnClick();

        if(StringUtils.isEmpty(feedContent.get())){
            ToastUtils.showShort("请输入反馈内容");
            return;
        } if(feedContent.get().length()>500){
            ToastUtils.showShort("反馈内容过长，最多500字");
            return;
        }
        uplaodFeedback();
    }

    private void uplaodFeedback(){

        FeedbackEntity entity = new FeedbackEntity();
        entity.setContent(feedContent.get());
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(entity));

        addSubscribe(model.uploadFeedback(body)
                        .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                        .compose(RxUtils.schedulersTransformer())
                        .compose(RxUtils.exceptionTransformer())
                        .doOnSubscribe(disposable -> showDialog("正在上传意见反馈..."))
                        .subscribe((Consumer<CollectResponse>) response -> {

                            dismissDialog();
                            KLog.e("uplaodFeedback", response.toString());
                            if (response.getCode() == 0) {
                                ToastUtils.showShort("上传意见反馈成功");
                                finish();
                            } else {
                                ToastUtils.showShort("服务器:" + response.getMsg());
                            }
                        }, (Consumer<ResponseThrowable>) throwable -> {
                            dismissDialog();
                            ToastUtils.showShort(throwable.message);
                        })
        );
    }
}
