package net.micode.notes.ui;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.appwidget.AppWidgetManager;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.style.BackgroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.TextNote;
import net.micode.notes.model.WorkingNote;
import net.micode.notes.model.WorkingNote.NoteSettingChangedListener;
import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser;
import net.micode.notes.tool.ResourceParser.TextAppearanceResources;
import net.micode.notes.ui.DateTimePickerDialog.OnDateTimeSetListener;
import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener;
import net.micode.notes.widget.NoteWidgetProvider_2x;
import net.micode.notes.widget.NoteWidgetProvider_4x;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoginActivity extends Activity{
    private  EditText accountEdit;

    private  EditText passwordEdit;

    private Button login;

    private Button cancel;

    private Button change;

    private long last_login_time = 0 ;

    public static final long MAX_LOGIN_TIME = 9999999;





    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        SharedPreferences pref=getSharedPreferences("user management",MODE_PRIVATE);
        boolean user_boolean = pref.getBoolean("user",false);//获取用户是否设置了密码
        if(!user_boolean) //User_boolean = false时，（没有设置密码），直接跳转到便签主界面
        {
            SharedPreferences.Editor editor=getSharedPreferences("user management", MODE_PRIVATE).edit();
            editor.putString("password","123456");
            editor.putBoolean("user",true);
            editor.apply();
        }

        SharedPreferences login_time_get = getSharedPreferences("login time",MODE_PRIVATE);
        boolean is_first_login = login_time_get.getBoolean("is_first_login",false);

        setContentView(R.layout.login_activity);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        cancel = (Button) findViewById(R.id.cancel);
        change = (Button) findViewById(R.id.change_password);


        long current_time = System.currentTimeMillis();

        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, R.string.app_already_quit, Toast.LENGTH_LONG).show();
                finish();
            }
        });

        Long current_login_time = login_time_get.getLong("current login time",0);
        if((is_first_login)&&(current_time - current_login_time <= MAX_LOGIN_TIME)){
            SharedPreferences.Editor editor=getSharedPreferences("login time", MODE_PRIVATE).edit();
            editor.putLong("current login time",System.currentTimeMillis());
            editor.apply();
            Intent intent = new Intent(LoginActivity.this,NotesListActivity.class);
            startActivity(intent);
            finish();
        }

        else{
            if(!is_first_login){
                SharedPreferences.Editor editor=getSharedPreferences("login time", MODE_PRIVATE).edit();
                editor.putLong("current login time",System.currentTimeMillis());
                editor.putBoolean("is_first_login",true);
                editor.apply();
            }

            login.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    String account = accountEdit.getText().toString();
                    String password = passwordEdit.getText().toString();

                    SharedPreferences pref=getSharedPreferences("user management",MODE_PRIVATE);
                    String share_password=pref.getString("password","");

                    if(account.equals("hei") && password.equals(share_password)){
                        ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
                        progressDialog.setTitle(R.string.Loading);
                        progressDialog.setMessage("Loading...");
                        progressDialog.setCancelable(true);
                        progressDialog.show();;
                        Intent intent = new Intent(LoginActivity.this,NotesListActivity.class);
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(LoginActivity.this, R.string.invalid,Toast.LENGTH_SHORT).show();
                    }
                }
            });

            change.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(LoginActivity.this,ChangeLoginPasswordActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
    }
}
