package com.example.logistics.ui;

import com.example.logistics.R;
import com.example.logistics.manager.goodManager;
import com.example.logistics.manager.operationManager;
import com.example.logistics.manager.userManager;
import com.example.logistics.entity.User;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;


public class LoginActivity extends Activity {

    private int islogin = 0;
    private Button loginButton;
    private Button signUpButton;
    private Button forgetButton;
    private Button phonenumButton;
    private AlertDialog alert;
    private Button changeButton;
    private EditText fphonenum;
    private EditText fpassword;
    int flag = 0; //flag = 0 是用户名登录；=1是手机号

    // 调用Actvity
    private String TAG = "LoginActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loginButton = (Button) this.findViewById(R.id.LoginButton);
        signUpButton = (Button) this.findViewById(R.id.SignUpButton);
        forgetButton = (Button) this.findViewById(R.id.forget);
        phonenumButton = (Button) this.findViewById(R.id.phonenum);


        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);

        View view2 = View.inflate(LoginActivity.this, R.layout.forget, null);

        builder.setTitle("取件").setView(view2);

        alert = builder.create();

        changeButton = (Button) view2.findViewById(R.id.change);
        fphonenum = (EditText) view2.findViewById(R.id.fphonenum);
        fpassword = (EditText) view2.findViewById(R.id.fpassword);

        loginButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        login(v);
                        Log.d(TAG, "login");
                    }
                }
        );

        signUpButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
                        Log.d(TAG, "signup");
                        startActivity(intent);
                    }
                }
        );

        forgetButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alert.show();
                        changeButton.setOnClickListener(
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Log.d(TAG, "change password");
                                        change(view);
                                    }
                                }
                        );
                    }
                }
        );

        EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
        phonenumButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        userName.setHint("请输入手机号");
                        flag = 1;
                    }
                }
        );

    }

    public void change(View view){
        String phoneunm = fphonenum.getText().toString().trim();
        String password = fpassword.getText().toString().trim();
        new Thread() {
            @Override
            public void run() {
                userManager userManager = new userManager();

                int msg = userManager.change(phoneunm, password);

                Log.d(TAG, "change");
                if(msg != 1){
                    Log.d(TAG, "pickup,failed");
                    hand2.sendEmptyMessage(msg);
                    return;
                }
                Log.d(TAG, "change,success");

                hand2.sendEmptyMessage(msg);
            }
        }.start();
    }

    public void login(View view){
        EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
        EditText passWord = (EditText) this.findViewById(R.id.PassWordEdit);

        new Thread(){
            @Override
            public void run(){
                userManager userManager = new userManager();
                int msg = 0;
                if(flag == 0){
                    msg = userManager.login(userName.getText().toString().trim(), passWord.getText().toString().trim());
                }else if(flag == 1){
                    msg = userManager.loginByPhone(userName.getText().toString().trim(), passWord.getText().toString().trim());

                }
                Log.e("MAin", "msg");
                hand1.sendEmptyMessage(msg);
                if(msg == 1){
                    User owner = userManager.findUser(userName.getText().toString().trim());
                    Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
                    if(flag == 1){
                        intent.putExtra("user", userName.getText().toString().trim());
                    }else{
                        intent.putExtra("user", owner.getPhoneNum());
                    }
                    startActivity(intent);
                    finish();
                }
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler(){
        @Override
        public void handleMessage(Message msg){
            if(msg.what == 0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 1){
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                islogin = 1;
            }else if(msg.what == 2){
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 3){
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_SHORT).show();
            }
        }
    };
    @SuppressLint("HandlerLeak")
    final Handler hand2 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                Toast.makeText(LoginActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 1) {
                Toast.makeText(LoginActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
            }
        }
    };

}