package com.android.chmo.ui.activity;

import android.Manifest;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.EditText;

import com.android.chmo.R;
import com.android.chmo.app.SpManager;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.LoginRes;
import com.android.chmo.http.service.UserService;
import com.android.chmo.im.IMManager;
import com.android.chmo.model.LoginUser;
import com.android.chmo.ui.activity.me.ForgetPwdActivity;
import com.android.chmo.utils.CommonUtils;
import com.android.chmo.utils.LoginUtils;
import com.google.gson.Gson;
import com.netease.nim.uikit.support.permission.MPermission;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionDenied;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionGranted;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/30.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.phone)
    EditText phoneEdit;
    @BindView(R.id.password)
    EditText passwordEdit;

    @Override
    protected int getContentResId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        checkLocationPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String phone = SpManager.getInstance().getString("LoginPhone");
        String password = SpManager.getInstance().getString("LoginPwd");

        phoneEdit.setText(phone);
        passwordEdit.setText(password);
    }

    @OnClick(R.id.login)
    public void login() {
        final String phone = phoneEdit.getText().toString();
        final String password = passwordEdit.getText().toString();
        if(TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }

        if(!CommonUtils.isPhone(phone)) {
            showToast("手机号不正确");
            return;
        }

        if(TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        showLoading("登录中...");
        UserService.login(phone, password, new RequestCallback(){
            @Override
            public void onSuccess(String result) {
                hideLoading();
                LoginRes res = new Gson().fromJson(result, LoginRes.class);
                if(res.data != null) {
                    LoginUser user = new LoginUser();
                    user.setUser(res.data);
                    LoginUtils.saveLoginUser(user);
                    SpManager.getInstance().setString("LoginPhone", phone);
                    SpManager.getInstance().setString("LoginPwd", password);

                    IMManager.login();
                    openPage(MainActivity.class);
                    finish();
                }else {
                    showToast(TextUtils.isEmpty(res.desc) ? "登录失败" : res.desc);
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("登录失败");
            }
        });

    }

    @OnClick(R.id.forgetPwd)
    public void forgetPwd() {
            openPage(ForgetPwdActivity.class);
    }

    @OnClick(R.id.go_register)
    public void goRegister() {
        openPage(RegisterActivity.class);
    }

    @OnClick(R.id.container)
    public void containerClick() {
        CommonUtils.closeKeybord(phoneEdit,this);
    }



    private void checkLocationPermission() {
        MPermission.with(this)
                .setRequestCode(20)
                .permissions( new String[] {
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS}).request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @OnMPermissionGranted(20)
    public void locationPermissionSuccess(){

    }

    @OnMPermissionDenied(20)
    public void locationPermissionFailed(){
        showToast("定位所需权限未授权");
    }


}
