package com.example.user.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class DisplayMessageActivity extends AppCompatActivity {

    private Handler handlerCoil;
    private Handler handlerInputRegister;
    private Handler handlerDiscreteInput;

    private GlobalState state;

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

        // listener
        LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil"));
        LocalBroadcastManager.getInstance(this).registerReceiver(discreteInputMessageReceiver, new IntentFilter("readOnlyDiscreteInput"));
        LocalBroadcastManager.getInstance(this).registerReceiver(inputRegisterMessageReceiver, new IntentFilter("readOnlyInputRegister"));

        state = (GlobalState) getApplicationContext();

        Toast toast = Toast.makeText(getApplicationContext(), "This is my toast !", Toast.LENGTH_LONG);
        toast.show();
        //handler
        handlerCoil = new Handler();
        handlerCoil.post(refreshCoil);

        handlerInputRegister = new Handler();
        handlerInputRegister.post(refreshInputRegister);

        handlerDiscreteInput = new Handler();
        handlerDiscreteInput.post(refreshDiscreteInput);
    }

    Runnable refreshCoil = new Runnable() {
        @Override
        public void run() {
        Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
        serviceIntent.setAction("read.coil");
        serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
        serviceIntent.putExtra("extra.ip.port", state.getPort());
        serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.Q_COIL.getStartReference());
        serviceIntent.putExtra("extra.count", GlobalState.InputConfig.Q_COIL.getCount());
        serviceIntent.putExtra("extra.intent.name", "readOnlyCoil");
        getApplicationContext().startService(serviceIntent);

        handlerCoil.postDelayed(this, 2000);
        }
    };

    Runnable refreshDiscreteInput = new Runnable() {
        @Override
        public void run() {
            Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
            serviceIntent.setAction("read.discrete.input");
            serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
            serviceIntent.putExtra("extra.ip.port", state.getPort());
            serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.I_DI.getStartReference());
            serviceIntent.putExtra("extra.count", GlobalState.InputConfig.I_DI.getCount());
            serviceIntent.putExtra("extra.intent.name", "readOnlyDiscreteInput");
            getApplicationContext().startService(serviceIntent);

            handlerDiscreteInput.postDelayed(this, 5000);
        }
    };

    Runnable refreshInputRegister = new Runnable() {
        @Override
        public void run() {
            Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
            serviceIntent.setAction("read.input.register");
            serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
            serviceIntent.putExtra("extra.ip.port", state.getPort());
            serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.AI_IR.getStartReference());
            serviceIntent.putExtra("extra.count", GlobalState.InputConfig.AI_IR.getCount());
            serviceIntent.putExtra("extra.intent.name", "readOnlyInputRegister");
            getApplicationContext().startService(serviceIntent);

            handlerInputRegister.postDelayed(this, 1000);
        }
    };

    // on message receive, update the view with values
    private BroadcastReceiver coilMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Bundle bundle = intent.getExtras();
            boolean[] booleanArray = bundle.getBooleanArray("coilValues");

            TextView textView1 = findViewById(R.id.textView1);
            textView1.setText(new Boolean(booleanArray[0]).toString());

            TextView textView2 = findViewById(R.id.textView2);
            textView2.setText(new Boolean(booleanArray[1]).toString());

            TextView textView3 = findViewById(R.id.textView3);
            textView3.setText(new Boolean(booleanArray[2]).toString());

            TextView textView4 = findViewById(R.id.textView4);
            textView4.setText(new Boolean(booleanArray[3]).toString());

            TextView textView5 = findViewById(R.id.textView5);
            textView5.setText(new Boolean(booleanArray[4]).toString());

            TextView textView6 = findViewById(R.id.textView6);
            textView6.setText(new Boolean(booleanArray[5]).toString());

            TextView textView7 = findViewById(R.id.textView7);
            textView7.setText(new Boolean(booleanArray[6]).toString());

            TextView textView8 = findViewById(R.id.textView8);
            textView8.setText(new Boolean(booleanArray[7]).toString());
        }
    };

    private BroadcastReceiver inputRegisterMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Bundle bundle = intent.getExtras();
            ArrayList<Integer> integerArrayList = bundle.getIntegerArrayList("irValues");

            TextView textView9 = findViewById(R.id.textView9);
            textView9.setText(integerArrayList.get(0).toString());
        }
    };

    private BroadcastReceiver discreteInputMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Bundle bundle = intent.getExtras();
            boolean[] booleanArray = bundle.getBooleanArray("discreteInputValues");

            TextView textView10 = findViewById(R.id.textView10);
            textView10.setText(new Boolean(booleanArray[0]).toString());

            TextView textView11 = findViewById(R.id.h2_textview);
            textView11.setText(new Boolean(booleanArray[1]).toString());

            TextView textView12 = findViewById(R.id.h1_textview);
            textView12.setText(new Boolean(booleanArray[2]).toString());

            TextView textView13 = findViewById(R.id.textView13);
            textView13.setText(new Boolean(booleanArray[3]).toString());

            TextView textView14 = findViewById(R.id.textView14);
            textView14.setText(new Boolean(booleanArray[4]).toString());

            TextView textView15 = findViewById(R.id.textView15);
            textView15.setText(new Boolean(booleanArray[5]).toString());

            TextView textView16 = findViewById(R.id.textView16);
            textView16.setText(new Boolean(booleanArray[6]).toString());

            TextView textView17 = findViewById(R.id.textView17);
            textView17.setText(new Boolean(booleanArray[7]).toString());

            TextView textView18 = findViewById(R.id.textView18);
            textView18.setText(new Boolean(booleanArray[8]).toString());

            TextView textView19 = findViewById(R.id.textView19);
            textView19.setText(new Boolean(booleanArray[9]).toString());

            TextView textView20 = findViewById(R.id.textView20);
            textView20.setText(new Boolean(booleanArray[10]).toString());

            TextView textView21 = findViewById(R.id.textView21);
            textView21.setText(new Boolean(booleanArray[11]).toString());
        }
    };
}
