package com.sheep.gamegroup.module.login;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ReLoginNameAct extends BaseActivity {

    @BindView(R.id.login_name_box)
    EditText loginNameBox;

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

    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitleFinish(this)
                .setTitle(this, "修改登录名");
        String name = getIntent().getStringExtra("name");
        if (!TextUtils.isEmpty(name)) {
            loginNameBox.setText(name);
        }
    }

    @OnClick(R.id.submit_btn)
    public void doSubmit(View v) {
        if (TextUtils.isEmpty(loginNameBox.getText().toString().trim())) {
            G.shortToast("请填写登录名");
            return;
        }
        if (!StringUtils.isUserName(loginNameBox.getText().toString().trim())) {
            G.shortToast("登录名只能包含英文、数字、._-@符号，长度4-20");
            return;
        }
        showProgress();
        JSONObject json = new JSONObject();
        json.put("user_name", loginNameBox.getText().toString().trim());
        SheepApp.getInstance().getNetComponent().getApiService().changeBaseInfo(json)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        hideProgress();
                        DataUtil.getInstance().setUserLoginName(loginNameBox.getText().toString().trim());
                        finish();
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        hideProgress();
                        G.shortToast(baseMessage);
                    }
                });
    }
}
