package com.wisewoods.eduandroid.ui;

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

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.base.BaseActivity;
import com.wisewoods.base.bean.LoginUser;
import com.wisewoods.base.bean.UserToken;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.utils.$;
import com.wisewoods.base.utils.DialogUtils;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityLoginBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;


public class LoginActivity extends BaseActivity<ActivityLoginBinding> {

    @Override
    protected void initData(Bundle savedInstanceState) {
        BarUtils.setStatusBarColor(this, 0);

        binding.tvLogin.setOnClickListener(v -> {
            login();
        });

        if ($.spNoNull("jwtToken")) {
            ActivityUtils.startActivity(MainActivity.class);
            finish();
        }
    }

    private void login() {
        String name = binding.etName.getText().toString().trim();
        String pwd = binding.etPwd.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            $.toast("输入用户名");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            $.toast("输入密码");
            return;
        }

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        HashMap<String, String> map = new HashMap<>();
        map.put("username", name);
        map.put("password", pwd);
        RequestBody body = RequestBody.create(GsonUtils.toJson(map), JSON);

        showLoading();
        App.api.login(body)
                .enqueue(new JCallback<UserToken>() {
                    @Override
                    public void onS(Response<UserToken> response, UserToken userToken) {
                        SPStaticUtils.put("jwtToken",
                                userToken.data.access_token, true);

                        App.api.getInfo().enqueue(new JCallback<LoginUser>() {
                            @Override
                            public void onS(Response<LoginUser> response, LoginUser loginUser) {

                                if (loginUser.user.getRolesStr().size() > 1) {
                                    hideLoading();
                                    DialogUtils.showList("请选择身份", loginUser.user.getRolesStr(), (materialDialog, index, text) -> {
                                        loginUser.user.roleIndex = index;
                                        UserManager.initUser(loginUser);
                                        showLoading();
                                        initPush();
                                        return null;
                                    });
                                } else {
                                    UserManager.initUser(loginUser);
                                    initPush();
                                }
                            }

                            @Override
                            public void onFailure(Call<LoginUser> call, Throwable t) {
                                super.onFailure(call, t);
                                hideLoading();
                            }
                        });
                    }

                    @Override
                    public void onFailure(Call<UserToken> call, Throwable t) {
                        super.onFailure(call, t);
                        hideLoading();
                    }
                });

    }

    private void initPush() {
        List<XGPushManager.AccountInfo> accountInfoList = new ArrayList<>();
        accountInfoList.add(new XGPushManager.AccountInfo(XGPushManager.AccountType.UNKNOWN.getValue(),
                UserManager.getUid()));
        XGPushManager.upsertAccounts(this, accountInfoList, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object o, int i) {
                hideLoading();
                ActivityUtils.startActivity(MainActivity.class);
                finish();
            }

            @Override
            public void onFail(Object o, int i, String s) {
                hideLoading();
                $.toast("登录失败,推送服务注册失败" + s);
            }
        });
    }
}