package cn.dankal.yunlaboratory.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.mvp.BasePresenter;
import cn.dankal.basiclib.bus.InfoChange;
import cn.dankal.basiclib.eventbus.AppBus;
import cn.dankal.basiclib.model.BaseModel;
import cn.dankal.basiclib.model.UserInfoBody;
import cn.dankal.basiclib.model.UserModel;
import cn.dankal.basiclib.rx.MapFunc;
import cn.dankal.basiclib.rx.NormalSubscriber;
import cn.dankal.yunlaboratory.R;
import cn.dankal.yunlaboratory.ui.widget.MainButton;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ChangeNicknameActivity extends BaseActivity {
    @BindView(R.id.iv_onback)
    ImageView ivOnback;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_result)
    EditText etResult;
    @BindView(R.id.btn_save)
    MainButton btnSave;

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_nickname;
    }

    @Override
    protected void initComponents() {
        btnSave.setEnabled(true);
        tvTitle.setText("昵称");
    }


    @OnClick({R.id.iv_onback, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_onback:
                finish();
                break;
            case R.id.btn_save:
                String nameStr = etResult.getText().toString().trim();

                if (!TextUtils.isEmpty(nameStr)){
                    updateName(nameStr);
                }
                break;
        }
    }

    public void updateName(String name) {
        UserInfoBody userInfoBody = new UserInfoBody();
        userInfoBody.setUpdate_type("name");
        userInfoBody.setName(name);
        HashMap<String,RequestBody> map=new HashMap<>();
        RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"),name);
        map.put("name",body);

        RequestBody updateTypeBody=RequestBody.create(MediaType.parse("multipart/form-data"),"name");
        map.put("update_type",updateTypeBody);
        UserServiceFactory.updateName("name", name).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel>() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoadingDialog();
            }

            @Override
            public void onNext(BaseModel stringBaseModel) {

                UserModel userModel = DKUserManager.getUserInfo();

                userModel.setName(name);
                DKUserManager.updateUserInfo(userModel);

                AppBus.getInstance().post(new InfoChange());

                finish();

            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissLoadingDialog();
            }
        });

    }

}
