package com.sheep.gamegroup.view.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerReNameComponent;
import com.sheep.gamegroup.di.modules.ReNameModule;
import com.sheep.gamegroup.presenter.ReNameContract;
import com.sheep.gamegroup.presenter.ReNamePresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
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 java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_INFO_NAME_MODIFY;

/**
 * Created by Administrator on 2018/3/24.
 */

public class ReNameActivity extends BaseActivity implements ReNameContract.View {


    @BindView(R.id.et)
    EditText et;

    @Inject
    ReNamePresenter presenter;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

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

    @Override
    public void initView() {
        TitleBarUtils
                .getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "修改昵称")
                .setTitleFinish(this);
        DaggerReNameComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .reNameModule(new ReNameModule(this))
                .build().inject(this);

    }

    @Override
    public void initListener() {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void initData() {
        Intent i = getIntent();
        String name;
        if (i != null && i.hasExtra("name") && !android.text.TextUtils.isEmpty(name = i.getStringExtra("name"))) {
            et.setText(name);
            et.setSelection(name.length());
        }

    }


    @Override
    public void NetSuccess(int code, String msg, String string) {
        G.showToast(msg);
        DataUtil.getInstance().setUserNickName(et.getText().toString());
        finish();
    }

    @Override
    public void NetError(int code, String errorMsg) {
        G.showToast(errorMsg);
        hideProgress();
    }

    @Override
    public void showTask() {

    }

    @Override
    public void showDialog() {
        showProgress();
    }

    @Override
    public void hideDialog() {
        hideProgress();
    }


    @OnClick(R.id.tv_submit)
    public void onClick() {
        USER_INFO_NAME_MODIFY.onEvent();
        if(android.text.TextUtils.isEmpty(et.getText().toString())){
            G.showToast("请输入昵称");
            return;
        }

        if(!CommonUtil.getInstance().handleText(et.getText().toString(), 12)){
            G.showToast("昵称长度要小于6个字符");
            return;
        }
        HashMap<String,String>  map=new HashMap<>();
        map.put("nickname", et.getText().toString());
        presenter.getTask(map);

    }
}
