package com.cooleshow.teacher.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.contract.ModifyNickNameContract;
import com.cooleshow.teacher.databinding.ActivityModifyNicknameBinding;
import com.cooleshow.teacher.presenter.mine.ModifyNickNamePresenter;
import com.cooleshow.usercenter.bean.SetDetailBean;

import org.json.JSONException;
import org.json.JSONObject;

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/5/16 17:22
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.MineCenter.MINE_MODIFY_NICKNAME)
public class ModifyNickNameActivity extends BaseMVPActivity<ActivityModifyNicknameBinding, ModifyNickNamePresenter> implements ModifyNickNameContract.ModifyNickNameView, View.OnClickListener {

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right_text:
                String userName = viewBinding.etNickname.getText().toString().trim();
                if (TextUtils.isEmpty(userName)){
                    ToastUtil.getInstance().showShort("请输入昵称！");
                    break;
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username", userName);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


                presenter.submitSetDetail(jsonObject.toString());
                break;
            case R.id.im_close:
                viewBinding.etNickname.setText("");
                break;

        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
    }
    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "修改昵称");
        viewBinding.toolbarInclude.tvRightText.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        viewBinding.toolbarInclude.tvRightText.setVisibility(View.VISIBLE);
        viewBinding.toolbarInclude.tvRightText.setText("保存");
        viewBinding.toolbarInclude.tvRightText.setOnClickListener(this);
        String userName = getIntent().getStringExtra("userName");
        if (!TextUtils.isEmpty(userName)){
            viewBinding.etNickname.setText(userName);
        }
        viewBinding.imClose.setOnClickListener(this);
    }

    @Override
    protected ActivityModifyNicknameBinding getLayoutView() {
        return ActivityModifyNicknameBinding.inflate(getLayoutInflater());
    }

    @Override
    protected ModifyNickNamePresenter createPresenter() {
        return new ModifyNickNamePresenter();
    }

    @Override
    public void submitSetDetailSuccess(SetDetailBean setDetailBean) {
        Intent intent=new Intent();
        intent.putExtra("userName",setDetailBean.username);
        setResult(RESULT_OK,intent);
        finish();
    }
}
