package com.sunzee.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.base.MvpActivity;
import com.sunzee.mvp.login.LoginPresenter;
import com.sunzee.mvp.login.LoginView;
import com.sunzee.utils.ToastUtil;

/**
 * 登录界面 activity
 */
public class LoginActivity extends MvpActivity<LoginPresenter> implements LoginView, View.OnClickListener {

    private EditText mEtUserName;
    private EditText mEtUserPassword;
    private TextView mTvUuserLogin;
    private View mLlHideKeyMap;
    private TextView mTvBak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }


    private void initView() {
        mEtUserName = findViewById(R.id.et_username);
        mEtUserPassword = findViewById(R.id.et_userpassword);
        mEtUserPassword.requestFocus();
        mTvUuserLogin = findViewById(R.id.tv_userlogin);
        mLlHideKeyMap = findViewById(R.id.ll_hidekeymap);
        mTvBak = findViewById(R.id.tv_back);
        mTvUuserLogin.setOnClickListener(this);
        mLlHideKeyMap.setOnClickListener(this);
        mTvBak.setOnClickListener(this);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void loginSuccessful() {
        ToastUtil.showToast("登录成功！");
        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void loginUserFailed() {
        ToastUtil.showToast("用户名或者密码错误,登录失败！！！");
    }

    @Override
    public void inputEmpty() {
        ToastUtil.showToast("请输入账户名或者密码！！");
    }

    @Override
    public void laterInput(int time) {
        ToastUtil.showToast("请在" + time + "秒后输入！");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_hidekeymap:
                //隐藏键盘
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                break;
            case R.id.tv_userlogin:
                //登录功能，只需要提供字符串就可以进行一个判断。
                mvpPresenter.login(mEtUserName.getText().toString().trim(), mEtUserPassword.getText().toString().trim());
                break;
            case R.id.tv_back:
                gotoAdvertisingActivity();
                break;
        }
    }

    private void gotoAdvertisingActivity() {
        startActivity(new Intent(LoginActivity.this, AdvertisingActivity.class));
        finish();
    }

}
