package com.github.nkzawa.socketio.androidchat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CheckBox;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Typeface;

import android.content.Context;

/**
 * A login screen that offers login via username.
 */
public class LoginActivity extends Activity implements View.OnClickListener{

    private EditText mUsernameView;

    private String mUsername;

    private CheckBox ageRequirement;
    private boolean metAgeReq;
    private TextView mainTitle;
    private NavigationView mNavigationView;

    private Socket mSocket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ChatApplication app = (ChatApplication) getApplication();
        mSocket = app.getSocket();
        //Main Title
        mainTitle = (TextView) findViewById(R.id.mainHeader);
        Typeface font = Typeface.createFromAsset(getAssets(),"fonts/Quantify Bold v2.6.ttf");
        mainTitle.setTypeface(font);

        // Set up the login form.
        mUsernameView = (EditText) findViewById(R.id.username_input);

        //Age Requirement
        ageRequirement = (CheckBox)findViewById(R.id.age_confirmation);

        ageRequirement.setOnClickListener(this);
        mUsernameView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_NULL) {
                    attemptLogin();
                    return true;
                }
                return false;
            }
        });

        Button signInButton = (Button) findViewById(R.id.sign_in_button);
        signInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();
            }
        });

        mSocket.on("login", onLogin);
    }
    //Checkbox for age Requirement
    public void onClick(View view){
        CheckBox ageRequirement = (CheckBox)view;
        if(ageRequirement.isChecked()){
            ageRequirement.setError(null);
            metAgeReq = true;
        }else{
            metAgeReq = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mSocket.off("login", onLogin);
    }
    /**
     * Attempts to sign in the account specified by the login form.
     * If there are form errors (invalid username, missing fields, etc.), the
     * errors are presented and no actual login attempt is made.
     */
    private void attemptLogin() {
        // Reset errors.
        mUsernameView.setError(null);
        ageRequirement.setError(null);

        // Store values at the time of the login attempt.
        String username = mUsernameView.getText().toString().trim();

        // Check for a valid username.
        if (TextUtils.isEmpty(username)) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            mUsernameView.setError(getString(R.string.error_field_required));
            mUsernameView.requestFocus();

            return;
        }
        if(!metAgeReq){
            ageRequirement.setError(getString(R.string.error_field_ageRequired));
            ageRequirement.requestFocus();
            return;
        }

        mUsername = username;

        //Sending the username to the MainActivity class as an intent to be
        //used for the menu
        Intent menu = new Intent(this, MainActivity.class);
        menu.putExtra("username", username);
        startActivity(menu);

        // perform the user login attempt.
        mSocket.emit("add user", username);
    }

    private Emitter.Listener onLogin = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            JSONObject data = (JSONObject) args[0];

            int numUsers;
            try {
                numUsers = data.getInt("numUsers");
            } catch (JSONException e) {
                return;
            }


            Intent intent = new Intent();
            intent.putExtra("username", mUsername);
            intent.putExtra("numUsers", numUsers);
            setResult(RESULT_OK, intent);
            finish();
        }
    };
}


@@ -644,41 +644,8 @@
 e;%0A%0A
-import android.content.Context;%0A%0A
 /**%0A

Update LoginActivity.java