package com.olmatix.ui.activity;

/**
 * Created by Lesjaw on 07/01/2017.
 */

import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.olmatix.lesjaw.olmatix.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SignupActivity extends AppCompatActivity {
    private static final String TAG = "SignupActivity";

    @InjectView(R.id.input_name) EditText _nameText;
    @InjectView(R.id.input_email) EditText _emailText;
    @InjectView(R.id.input_password) EditText _passwordText;
    @InjectView(R.id.btn_signup) Button _signupButton;
    @InjectView(R.id.link_login) TextView _loginLink;
    String name, email, password;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ButterKnife.inject(this);

        _signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signup();
            }
        });

        _loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Finish the registration screen and return to the Login activity
                finish();
            }
        });
    }

    public void signup() {
        Log.d(TAG, "Signup");

        if (!validate()) {
            onSignupFailed();
            return;
        }

        _signupButton.setEnabled(false);

        final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Creating Account...");
        progressDialog.show();

        name = _nameText.getText().toString();
        email = _emailText.getText().toString();
        password = _passwordText.getText().toString();

        // TODO: Implement your own signup logic here.

        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        // On complete call either onSignupSuccess or onSignupFailed
                        // depending on success
                        onSignupSuccess();
                        progressDialog.dismiss();
                    }
                }, 3000);
    }


    public void onSignupSuccess() {
        _signupButton.setEnabled(true);
        setResult(RESULT_OK, null);
        sendEmail();
        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();

        _signupButton.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String name = _nameText.getText().toString();
        String email = _emailText.getText().toString();
        String password = _passwordText.getText().toString();

        if (name.isEmpty() || name.length() < 3) {
            _nameText.setError("at least 3 characters");
            valid = false;
        } else {
            _nameText.setError(null);
        }

        if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            _emailText.setError("enter a valid email address");
            valid = false;
        } else {
            _emailText.setError(null);
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
            _passwordText.setError("between 4 and 10 alphanumeric characters");
            valid = false;
        } else {
            _passwordText.setError(null);
        }

        return valid;
    }

    private void sendEmail() {
        Intent intent = new Intent(Intent.ACTION_SENDTO)
                .setData(new Uri.Builder().scheme("mailto").build())
                .putExtra(Intent.EXTRA_EMAIL, new String[]{ "Olmatix <lesjaw@olmatix.com>" })
                .putExtra(Intent.EXTRA_SUBJECT, "Request Auth for Login")
                .putExtra(Intent.EXTRA_TEXT, "Your name : " + name +"\n" +" Your email : "+ email +"\n"+" Your password : "+password
                +"\n" +"\n"+" This is your Auth Login request, we will send you a confirmation email as soon as possible.."
                +"\n"+ " Send this email now.. thank you..")
                ;

        ComponentName emailApp = intent.resolveActivity(getPackageManager());
        ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
        if (emailApp != null && !emailApp.equals(unsupportedAction))
            try {
                // Needed to customise the chooser dialog title since it might default to "Share with"
                // Note that the chooser will still be skipped if only one app is matched
                Intent chooser = Intent.createChooser(intent, "Send email with");
                startActivity(chooser);
                return;
            }
            catch (ActivityNotFoundException ignored) {
            }

        Toast
                .makeText(this, "Couldn't find an email app and account", Toast.LENGTH_LONG)
                .show();
    }
}
@@ -1026,16 +1026,51 @@
 assword;
+%0A    ProgressDialog progressDialog;
 %0A%0A    @O
@@ -1920,29 +1920,8 @@
     
-final ProgressDialog 
 prog
@@ -2323,24 +2323,54 @@
 e.%0A%0A        
+onSignupSuccess();%0A%0A        /*
 new android.
@@ -2648,58 +2648,8 @@
 ();%0A
-                        progressDialog.dismiss();%0A
     
@@ -2691,16 +2691,18 @@
 , 3000);
+*/
 %0A    %7D%0A%0A
@@ -2810,24 +2810,58 @@
 _OK, null);%0A
+        progressDialog.dismiss();%0A
         send
@@ -2933,24 +2933,26 @@
 ) %7B%0A        
+//
 Toast.makeTe
@@ -2973,20 +2973,19 @@
 ext(), %22
-Logi
+SIg
 n failed
@@ -4522,20 +4522,16 @@
     +%22%5Cn
-%22 +%22
 %5Cn%22+%22 Th
@@ -4642,16 +4642,18 @@
     +%22%5Cn
+%5Cn
 %22+ %22 Sen
@@ -4674,39 +4674,32 @@
 ow..
- thank you..%22)%0A                
+%22+%22%5Cn%5Cn%22+%22 Thank you..%22)
 ;%0A%0A 

add login page and sign up