package com.example.cici.voice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.cici.voice.R;
import com.example.cici.bean.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {
    private EditText log_user,log_pasd;
    private BmobUser user;
    private Button btn_log,btn_reg;
    String name,password;
    boolean ishave=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        log_user = (EditText) findViewById(R.id.et_username);
        log_pasd = (EditText) findViewById(R.id.et_pwd);
        btn_log=findViewById(R.id.btn_login);
        user=BmobUser.getCurrentUser();
        if (user!=null){
            Intent intent = new Intent();
            intent.setClass(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });

    }


    public void toast(String msg) {
        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
    private void login() {
        // TODO Auto-generated method stub
        name=log_user.getText().toString();
        password=log_pasd.getText().toString();
        if (name.isEmpty()) {
            toast("用户名为空");
            return;
        }
        if (password.isEmpty()) {
            toast("密码为空");
            return;
        }
        // isChecked = true;


        User user = new User();
        user.setUsername(name);
        user.setPassword(password);
            user.login(new SaveListener<BmobUser>() {
                @Override
                public void done(BmobUser bmobUser, BmobException e) {
                    if(e==null){
                        toast("登录成功！");
                        Intent intent = new Intent();
                        intent.setClass(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }else{
                        toast("登录失败！ " + e.toString());
                    }
                }
            });

       

    }
    public void OnMyRegistClick(View v)  {
        Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
        //intent.putExtra("info", "No66778899");
        LoginActivity.this.startActivity(intent);
    }

    /**
     * Jump to reset password interface
     * @param
     */
   /* public void OnMyResPwdClick(View v){
        Intent intent=new Intent(LoginActivity.this,ResPwdActivity.class);
        LoginActivity.this.startActivity(intent);
    }*/


    private boolean isHaveUser(String user){

        BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
        query.addWhereEqualTo("username", user);
        query.findObjects(new FindListener<BmobUser>() {
            @Override
            public void done(List<BmobUser> object, BmobException e) {
                if(e==null){
                    toast("查询用户成功:"+object.size());

                    ishave=true;
                }else{
                    toast("查询用户信息失败:" + e.getMessage());
                }
            }
        });
        return ishave;
    }
}
