package com.android.chmo.ui.activity.me;

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

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.Res;
import com.android.chmo.http.service.UserService;
import com.android.chmo.model.LoginUser;
import com.android.chmo.ui.view.TopBar;
import com.google.gson.Gson;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/8/6.
 */

public class ModifyNameActivity extends BaseActivity {
    @BindView(R.id.topBar)
    TopBar topBar;

    @BindView(R.id.input)
    EditText editText;

    @Override
    protected int getContentResId() {
        return R.layout.activity_modify_name;
    }

    @Override
    public void initView() {
        LoginUser user = ChmoApplication.getApp().getLoginUser();
        editText.setText(user.pet);

        topBar.setRightBtn("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
    }

    public void save() {
        String name = editText.getText().toString();
        if(TextUtils.isEmpty(name)) {
            showToast("请输入昵称");
            return;
        }

        showLoading();
        UserService.modifyName(name, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                Res res = new Gson().fromJson(result,Res.class);
                if(res.msg.equals("success")) {
                    showToast("修改成功");
                    setResult(RESULT_OK);
                    finish();
                }else {
                    showToast("修改失败");
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("修改失败");
            }
        });
    }
}
