package com.cwbuyer.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cwbuyer.lib.DateUtil;
import com.cwbuyer.lib.PrefKey;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

public class APassword extends Activity{

	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		
		setContentView(R.layout.password);
		((Button)findViewById(R.id.btn_ok)).setOnClickListener(new OnClickListener(){
			public void onClick(View view){
				Utilis.runVibrate(APassword.this);
				EditText edit = (EditText)APassword.this.findViewById(R.id.edit_password);
				String password = edit.getText().toString();
				String userpass = Utilis.getStringSet(APassword.this, PrefKey.PASSWORD, "");
				if(password != null && password.length() > 0){
					if(password.equalsIgnoreCase(userpass)){
						Toast.makeText(APassword.this, "Master! Nice to see You again!!", Toast.LENGTH_SHORT).show();
						Utilis.putIni(APassword.this,"SYS","DEPT","0",5);
					}else if (password.equalsIgnoreCase(DateUtil.getSystemTime().substring(2,11)+"668")){
						Toast.makeText(APassword.this, "Engineer! Nice to see You!", Toast.LENGTH_SHORT).show();
						Utilis.putIni(APassword.this,"SYS","DEPT","0",5);
					}else if(password.equalsIgnoreCase(userpass.substring(0, 3))){
						Toast.makeText(APassword.this, "Accounter! Nice to see You again!!", Toast.LENGTH_SHORT).show();
						Utilis.putIni(APassword.this,"SYS","DEPT","2",5);
					}else {
						Toast.makeText(APassword.this, "Have a GoodDay!!", Toast.LENGTH_SHORT).show();
						Utilis.putIni(APassword.this,"SYS","DEPT","1",5);
					}
				}else{
					Toast.makeText(APassword.this, "Have a GoodDay!!", Toast.LENGTH_SHORT).show();
					Utilis.putIni(APassword.this,"SYS","DEPT","1",5);
				}
				APassword.this.setResult(RESULT_OK);
				finish();
			}
		});
		
		((Button)findViewById(R.id.btn_cancel)).setOnClickListener(new OnClickListener(){
			public void onClick(View view){
				try{
					Utilis.runVibrate(APassword.this);
					 new AlertDialog.Builder(APassword.this).setTitle(getString(R.string.str_msg)).setMessage(getString(R.string.msg_exit)).setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
				            public void onClick(DialogInterface dialog, int which) {
				              finish();
				            }
				          }).setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
				            public void onClick(DialogInterface dialog, int which) {
				            }
				          }).show();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
          new AlertDialog.Builder(APassword.this).setTitle(getString(R.string.str_msg)).setMessage(getString(R.string.msg_exit)).setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              finish();
            }
          }).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
          }).show();
          return true;
        } else {
          return super.onKeyDown(keyCode, event);
        }
    }
}
