package com.wisewoods.eduandroid.ui;

import android.os.Bundle;
import android.text.TextUtils;

import com.blankj.utilcode.util.KeyboardUtils;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.base.BaseActivity;
import com.wisewoods.base.bean.Results;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.utils.$;
import com.wisewoods.base.utils.ImgUtils;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityUserInfoBinding;
import com.wisewoods.eduandroid.oss.ICloundOS;
import com.wisewoods.eduandroid.oss.OSSManager;

import retrofit2.Response;

public class UserInfoActivity extends BaseActivity<ActivityUserInfoBinding> {

    String avater;

    @Override
    protected void initData(Bundle savedInstanceState) {
        showBarBack(binding.bar.tvTitle, "用户信息", binding.bar.ivBack);

        binding.ivAvater.setOnClickListener(v -> selectAvater());
        binding.tvAvater.setOnClickListener(v -> selectAvater());
        binding.tvPut.setOnClickListener(v -> put());

        binding.etNick.setText(UserManager.getUser().nickName);
        binding.tvName.setText(UserManager.getUser().userName);
        binding.tvPhone.setText(UserManager.getUser().phonenumber);
        ImgUtils.load(activity, UserManager.getAvatar(), binding.ivAvater);
    }

    private void put() {
        String name = binding.etNick.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            $.toast("输入姓名");
            return;
        }
        App.api.selfUpdate(avater, name).enqueue(new JDCallback<Results>() {
            @Override
            public void onS(Response<Results> response, Results results) {
                UserManager.getUser().avatar = avater;
                UserManager.getUser().nickName = name;
                $.toast("修改成功");
                binding.etNick.clearFocus();
            }
        });

    }


    private void selectAvater() {
        KeyboardUtils.hideSoftInput(this);
        showLoading();
        OSSManager.getInstance().selectAvater(this, OSSManager.OSSPATH, (code, msg) -> {
            if (code == ICloundOS.CODE_SUCCESS) {
                ImgUtils.load(activity, msg, binding.ivAvater);
                avater = msg;
            } else {
                $.toast(msg);
            }
            hideLoading();
        });
    }
}
