package com.example.jorgepalacios.threewayhandshake;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button firstB = (Button) findViewById(R.id.button);
        Button secondB = (Button) findViewById(R.id.button2);
        Button cCheck = (Button) findViewById(R.id.connectionC);
        final Button cTerminate = (Button) findViewById(R.id.terminate);

        final TextView requestText = (TextView) findViewById(R.id.requestPt1);
        final TextView responseText = (TextView) findViewById(R.id.responsePt1);
        final TextView ackText = (TextView) findViewById(R.id.acknowledgePt1);
        final TextView conStatus = (TextView) findViewById(R.id.cStatus);

        final EditText firstT = (EditText) findViewById(R.id.editText);
        final EditText secondT = (EditText) findViewById(R.id.editText2);


        final HostA request = new HostA();
        final HostB responds = new HostB();


        //Bundle
        firstB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fText = firstT.getText().toString();
                //responds.recieve(Integer.parseInt(fText));
                request.setSequenceNum(Integer.parseInt(fText));
                requestText.setText(responds.recieve(Integer.parseInt(fText)));
                requestText.setVisibility(View.VISIBLE);

            }
        });

        secondB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String sText = secondT.getText().toString();
                final String fText = firstT.getText().toString();
                responds.setSequenceNum(Integer.parseInt(sText));
                responseText.setText(request.CheckSeq(responds.getAcknowledgeNum(), Integer.parseInt(sText), responds.getConnection()));
                responseText.setVisibility(View.VISIBLE);

                ackText.setText(responds.CheckSeq(request.getAcknowledgeNum(), request.getRespondSeqNum(), request.getConnection()));
                ackText.setVisibility(View.VISIBLE);
            }
        });

        cCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final char check = 'y';
                conStatus.setText(responds.connectionCheck(Character.toString(check)));
                conStatus.setVisibility(View.VISIBLE);
                cTerminate.setVisibility(View.VISIBLE);
            }
        });

        cTerminate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(MainActivity.this, TerminateActivity.class);
                intent.putExtra("HostA", request);
                intent.putExtra("HostB", responds);

                startActivity(intent);

            }
        });




    }




}
