package org.bandie.yipanic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;

import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;


public class CommandActivity extends AppCompatActivity {


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

    ImageButton b = findViewById(R.id.btnShutdown);
    b.setEnabled(false);
    b = findViewById(R.id.btnPanic);
    b.setEnabled(false);
    b = findViewById(R.id.btnInfraShutdown);
    b.setEnabled(false);
    b = findViewById(R.id.btnInfraPanic);
    b.setEnabled(false);

    Switch s = findViewById(R.id.swSecShutdown1);
    s.setChecked(false);
    s = findViewById(R.id.swSecShutdown2);
    s.setChecked(false);
    s = findViewById(R.id.swSecPanic1);
    s.setChecked(false);
    s = findViewById(R.id.swSecPanic2);
    s.setChecked(false);
    s = findViewById(R.id.swSecPanic3);
    s.setChecked(false);
    s = findViewById(R.id.swSecInfraShutdown1);
    s.setChecked(false);
    s = findViewById(R.id.swSecInfraShutdown2);
    s.setChecked(false);
    s = findViewById(R.id.swSecInfraPanic1);
    s.setChecked(false);
    s = findViewById(R.id.swSecInfraPanic2);
    s.setChecked(false);
    s = findViewById(R.id.swSecInfraPanic3);
    s.setChecked(false);

  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    System.out.println(item.getItemId());
    switch (item.getItemId()) {
      case 16908332:
      case R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    this.finish();
  }

  public void sendLockCmd(View view) {
    Command c = new Command(view, getApplicationContext());
    JsonObjectRequest send = c.lock();
    if (send != null)
      Volley.newRequestQueue(this).add(send);
  }

  public void sendShutdownCmd(View view) {
    Command c = new Command(view, getApplicationContext());
    JsonObjectRequest send = c.shutdown();
    if (send != null)
      Volley.newRequestQueue(this).add(send);
  }

  public void sendPanicCmd(View view) {
    Command c = new Command(view, getApplicationContext());
    JsonObjectRequest send = c.panic();
    if (send != null)
      Volley.newRequestQueue(this).add(send);
  }

  public void sendInfraShutdownCmd(View view) {
    Command c = new Command(view, getApplicationContext());
    JsonObjectRequest send = c.infraShutdown();
    if (send != null)
      Volley.newRequestQueue(this).add(send);
  }

  public void sendInfraPanicCmd(View view) {
    Command c = new Command(view, getApplicationContext());
    JsonObjectRequest send = c.infraPanic();
    if (send != null)
      Volley.newRequestQueue(this).add(send);
  }

  public void switchShutdown(View view) {
    final Switch s = findViewById(view.getId());
    Switch s1 = findViewById(R.id.swSecShutdown1);
    Switch s2 = findViewById(R.id.swSecShutdown2);
    ImageButton b = findViewById(R.id.btnShutdown);
    if (s1.isChecked() && s2.isChecked())
      b.setEnabled(true);
    else
      b.setEnabled(false);

    if (s.isChecked()) {
      Handler h = new Handler();
      h.postDelayed(new Runnable() {
        @Override
        public void run() {
          ImageButton b = findViewById(R.id.btnShutdown);
          s.setChecked(false);
          b.setEnabled(false);
        }
      }, 10000);
    }
  }

  public void switchPanic(View view) {
    final Switch s = findViewById(view.getId());
    Switch s1 = findViewById(R.id.swSecPanic1);
    Switch s2 = findViewById(R.id.swSecPanic2);
    Switch s3 = findViewById(R.id.swSecPanic3);
    ImageButton b = findViewById(R.id.btnPanic);
    if (s1.isChecked() && s2.isChecked() && s3.isChecked())
      b.setEnabled(true);
    else
      b.setEnabled(false);

    if (s.isChecked()) {
      Handler h = new Handler();
      h.postDelayed(new Runnable() {
        @Override
        public void run() {
          ImageButton b = findViewById(R.id.btnPanic);
          s.setChecked(false);
          b.setEnabled(false);
        }
      }, 10000);
    }
  }

  public void switchInfraShutdown(View view) {
    final Switch s = findViewById(view.getId());
    Switch s1 = findViewById(R.id.swSecInfraShutdown1);
    Switch s2 = findViewById(R.id.swSecInfraShutdown2);
    ImageButton b = findViewById(R.id.btnInfraShutdown);
    if (s1.isChecked() && s2.isChecked())
      b.setEnabled(true);
    else
      b.setEnabled(false);

    if (s.isChecked()) {
      Handler h = new Handler();
      h.postDelayed(new Runnable() {
        @Override
        public void run() {
          ImageButton b = findViewById(R.id.btnInfraShutdown);
          s.setChecked(false);
          b.setEnabled(false);
        }
      }, 10000);
    }
  }

  public void switchInfraPanic(View view) {
    final Switch s = findViewById(view.getId());
    Switch s1 = findViewById(R.id.swSecInfraPanic1);
    Switch s2 = findViewById(R.id.swSecInfraPanic2);
    Switch s3 = findViewById(R.id.swSecInfraPanic3);
    ImageButton b = findViewById(R.id.btnInfraPanic);
    if (s1.isChecked() && s2.isChecked() && s3.isChecked())
      b.setEnabled(true);
    else
      b.setEnabled(false);

    if (s.isChecked()) {
      Handler h = new Handler();
      h.postDelayed(new Runnable() {
        @Override
        public void run() {
          ImageButton b = findViewById(R.id.btnInfraPanic);
          s.setChecked(false);
          b.setEnabled(false);
        }
      }, 10000);
    }
  }


  public void startSettingsActivity(@SuppressWarnings("unused") View view) {
    Intent intent = new Intent(this, SettingsActivity.class);
    startActivity(intent);
  }

}
