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.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private Handler handlerCoil;
    private Handler handlerDiscreteInput;

    private GlobalState state;
    private static final Integer REFRESH_DELAY = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        state = (GlobalState) getApplicationContext();
        // listener
        LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil"));
        LocalBroadcastManager.getInstance(this).registerReceiver(discreteInputMessageReceiver, new IntentFilter("readOnlyDiscreteInput"));
        // handler
        handlerCoil = new Handler();
        handlerCoil.post(refreshCoil);

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

        setContentView(R.layout.activity_main);

        // swipe event
        OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(MainActivity.this) {
            @Override
            public void onSwipeLeft() {
                Intent nextIntent = new Intent(MainActivity.this, ManualDriveActivity.class);
                startActivity(nextIntent);
              //  handlerCoil.removeCallbacks(refreshCoil);
              //  handlerDiscreteInput.removeCallbacks(refreshDiscreteInput);
            }
        };

        final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        viewGroup.setOnTouchListener(onSwipeTouchListener);
    }

    @Override
    public void onPause () {
        super.onPause();
        handlerCoil.removeCallbacks(refreshCoil);
        handlerDiscreteInput.removeCallbacks(refreshDiscreteInput);
    }

    @Override
    public void onResume () {
        super.onResume();
        handlerCoil.post(refreshCoil);
        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, REFRESH_DELAY);
        }
    };

    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, REFRESH_DELAY);
        }
    };

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

            // ============= Light ===============
            ImageView pu1StatusLightOn = findViewById(R.id.pu1_status_light_on_mainview);
            ImageView pu1StatusLightOff = findViewById(R.id.pu1_status_light_off_mainview);
            ImageView pu2StatusLightOn = findViewById(R.id.pu2_status_light_on_mainview);
            ImageView pu2StatusLightOff = findViewById(R.id.pu2_status_light_off_mainview);

            ImageView sp1StatusLightOn = findViewById(R.id.sp1_status_light_on_mainview);
            ImageView sp1StatusLightOff = findViewById(R.id.sp1_status_light_off_mainview);
            ImageView b1StatusLightOn = findViewById(R.id.b1_status_light_on_mainview);
            ImageView b1StatusLightOff = findViewById(R.id.b1_status_light_off_mainview);

            ImageView m1StatusLightOn = findViewById(R.id.m1_status_light_on_mainview);
            ImageView m1StatusLightOff = findViewById(R.id.m1_status_light_off_mainview);
            ImageView m2StatusLightOn = findViewById(R.id.m2_status_light_on_mainview);
            ImageView m2StatusLightOff = findViewById(R.id.m2_status_light_off_mainview);

            // ============== Coil : Range 8192 - 8199 =================
            // 8199
            if (booleanArray[7]) {
                b1StatusLightOn.setVisibility(View.VISIBLE);
                b1StatusLightOff.setVisibility(View.INVISIBLE);
            } else {
                b1StatusLightOn.setVisibility(View.INVISIBLE);
                b1StatusLightOff.setVisibility(View.VISIBLE);
            }

            // 8194
            if (booleanArray[2]) {
                m1StatusLightOn.setVisibility(View.VISIBLE);
                m1StatusLightOff.setVisibility(View.INVISIBLE);
            } else {
                m1StatusLightOn.setVisibility(View.INVISIBLE);
                m1StatusLightOff.setVisibility(View.VISIBLE);
            }

            // 8195
            if (booleanArray[3]) {
                m2StatusLightOn.setVisibility(View.VISIBLE);
                m2StatusLightOff.setVisibility(View.INVISIBLE);
            } else {
                m2StatusLightOn.setVisibility(View.INVISIBLE);
                m2StatusLightOff.setVisibility(View.VISIBLE);
            }

            // 8196
            if (booleanArray[4]) {
                pu1StatusLightOn.setVisibility(View.VISIBLE);
                pu1StatusLightOff.setVisibility(View.INVISIBLE);
            } else {
                pu1StatusLightOn.setVisibility(View.INVISIBLE);
                pu1StatusLightOff.setVisibility(View.VISIBLE);
            }

            // 8197
            if (booleanArray[5]) {
                pu2StatusLightOn.setVisibility(View.VISIBLE);
                pu2StatusLightOff.setVisibility(View.INVISIBLE);
            } else {
                pu2StatusLightOn.setVisibility(View.INVISIBLE);
                pu2StatusLightOff.setVisibility(View.VISIBLE);
            }

            // 8198
            if (booleanArray[6]) {
                sp1StatusLightOn.setVisibility(View.VISIBLE);
                sp1StatusLightOff.setVisibility(View.INVISIBLE);
            } else {
                sp1StatusLightOn.setVisibility(View.INVISIBLE);
                sp1StatusLightOff.setVisibility(View.VISIBLE);
            }
        }
    };

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

            // Lights
            ImageView h1StatusLightOn = findViewById(R.id.h1_status_light_on_mainview);
            ImageView h1StatusLightOff = findViewById(R.id.h1_status_light_off_mainview);
            ImageView h2StatusLightOn = findViewById(R.id.h2_status_light_on_mainview);
            ImageView h2StatusLightOff = findViewById(R.id.h2_status_light_off_mainview);

            ImageView ind1StatusLightOn = findViewById(R.id.ind1_status_light_on_mainview);
            ImageView ind1StatusLightOff = findViewById(R.id.ind1_status_light_off_mainview);
            ImageView ls5StatusLightOn = findViewById(R.id.ls5_status_light_on_mainview);
            ImageView ls5StatusLightOff = findViewById(R.id.ls5_status_light_off_mainview);
            ImageView ls1StatusLightOn = findViewById(R.id.ls1_status_light_on_mainview);
            ImageView ls1StatusLightOff = findViewById(R.id.ls1_status_light_off_mainview);
            ImageView ls4StatusLightOn = findViewById(R.id.ls4_status_light_on_mainview);
            ImageView ls4StatusLightOff = findViewById(R.id.ls4_status_light_off_mainview);

            // ============ Discrete Input : Range 0 - 11 ========================
            // di 1
            if (booleanArray[1]) {
                h1StatusLightOff.setVisibility(View.VISIBLE);
            } else {
                h1StatusLightOff.setVisibility(View.INVISIBLE);
            }

            // di 2
            if (booleanArray[2]) {
                h1StatusLightOn.setVisibility(View.VISIBLE);
            } else {
                h1StatusLightOn.setVisibility(View.INVISIBLE);
            }

            // di 10
            if (booleanArray[10]) {
                h2StatusLightOff.setVisibility(View.VISIBLE);
            } else {
                h2StatusLightOff.setVisibility(View.INVISIBLE);
            }

            // di 9
            if (booleanArray[9]) {
                h2StatusLightOn.setVisibility(View.VISIBLE);
            } else {
                h2StatusLightOn.setVisibility(View.INVISIBLE);
            }

            // di 11
            if (booleanArray[11]) {
                ind1StatusLightOff.setVisibility(View.INVISIBLE);
                ind1StatusLightOn.setVisibility(View.VISIBLE);
            } else {
                ind1StatusLightOff.setVisibility(View.VISIBLE);
                ind1StatusLightOn.setVisibility(View.INVISIBLE);
            }

            // di 4
            if (booleanArray[4]) {
                ls5StatusLightOff.setVisibility(View.INVISIBLE);
                ls5StatusLightOn.setVisibility(View.VISIBLE);
            } else {
                ls5StatusLightOff.setVisibility(View.VISIBLE);
                ls5StatusLightOn.setVisibility(View.INVISIBLE);
            }

            // di 0
            if (booleanArray[0]) {
                ls1StatusLightOff.setVisibility(View.INVISIBLE);
                ls1StatusLightOn.setVisibility(View.VISIBLE);
            } else {
                ls1StatusLightOff.setVisibility(View.VISIBLE);
                ls1StatusLightOn.setVisibility(View.INVISIBLE);
            }

            // di 3
            if (booleanArray[3]) {
                ls4StatusLightOff.setVisibility(View.INVISIBLE);
                ls4StatusLightOn.setVisibility(View.VISIBLE);
            } else {
                ls4StatusLightOff.setVisibility(View.VISIBLE);
                ls4StatusLightOn.setVisibility(View.INVISIBLE);
            }
        }
    };
}

