package com.example.surveillanceapp;

import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;


public class ActivityCalendar extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

    boolean time= true;
     // String HOST="tcp://farmer.cloudmqtt.com:13237";
     // String USER="cqeopqjb";
     // String PASSWORD ="0ZilzPcY0rxh";

    String message;
    String message2;
    String topic;
    String topic2;

    CheckBox everyDay,monday,tuesday,wednesday,thursday,friday,saturday,sunday;

    @Override
    public void onBackPressed() {
        finish();
    }

          @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getSupportActionBar().setTitle("Scheduler");
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);

            setContentView(R.layout.activity_calendar);

            initCheckBox();

            Button btStart = findViewById(R.id.button_start);
            btStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    time = false;
                    DialogFragment timePicker = new TimePickerFragment();
                    timePicker.show(getSupportFragmentManager(), "timeStart picker");
                }
            });

            Button btEnd = findViewById(R.id.button_end);
            btEnd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    time = true;
                    DialogFragment timePicker = new TimePickerFragment();
                    timePicker.show(getSupportFragmentManager(), "timeEnd picker");
                }
            });

              Button btsetEnd = findViewById(R.id.btSetEnd);
              btsetEnd.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic2,message2);
                  }
              });

              Button btsetStart = findViewById(R.id.btSetStart);
              btsetStart.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,message);

                  }
              });

            Button btOFF = findViewById(R.id.btOFF);
            btOFF.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    topic="Rpi/";
                    message="scheduleOff";
                    ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,message);
                }
            });
        }

    public void initCheckBox (){

            everyDay = findViewById(R.id.checkDay);
            monday = findViewById(R.id.checkMo);
            tuesday = findViewById(R.id.checkTu);
            wednesday = findViewById(R.id.checkWe);
            thursday = findViewById(R.id.checkTh);
            friday = findViewById(R.id.checkFr);
            saturday = findViewById(R.id.checkSa);
            sunday = findViewById(R.id.checkSu);
        }

    public void onCheckboxClicked(View view) {

        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.checkDay:
                if (everyDay.isChecked()) {
                    setAllChecked(true);
                   Toast.makeText(this, "Every Day", Toast.LENGTH_SHORT).show();
                    topic="Rpi/EveryDay";
                    topic2="Rpi/EveryDayEnd";
                }
                else if (!everyDay.isChecked()) {
                    setAllChecked(false);
                }
                break;
            case R.id.checkMo:
                if (monday.isChecked()){
                  Toast.makeText(this, "Monday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Monday";
                    topic2="Rpi/MondayEnd";

                    setAllEnabled(false);
                    monday.setEnabled(true);

                }
                else if (!monday.isChecked()) {
                    setAllEnabled(true);

                }
                break;
            case R.id.checkTu:
                if (tuesday.isChecked()){
                 Toast.makeText(this, "Tuesday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Tuesday";
                    topic2="Rpi/TuesdayEnd";

                    setAllEnabled(false);
                    tuesday.setEnabled(true);
                }
                else if (!tuesday.isChecked()) {
                    setAllEnabled(true);

                }
                break;
            case R.id.checkWe:
                if (wednesday.isChecked()){
                  Toast.makeText(this, "Wednesday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Wednesday";
                    topic2="Rpi/WednesdayEnd";


                    setAllEnabled(false);
                    wednesday.setEnabled(true);
                }
                else if (!wednesday.isChecked()) {
                    setAllEnabled(true);
                }
                break;
            case R.id.checkTh:
                if (thursday.isChecked()){
                  Toast.makeText(this, "Thursday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Thursday";
                    topic2="Rpi/ThursdayEnd";


                    setAllEnabled(false);
                    thursday.setEnabled(true);
                }
                else if (!thursday.isChecked()) {
                    setAllEnabled(true);
                }
                break;
            case R.id.checkFr:
                if (friday.isChecked()){
                   Toast.makeText(this, "Friday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Friday";
                    topic2="Rpi/FridayEnd";

                    setAllEnabled(false);
                    friday.setEnabled(true);
                }
                else if (!friday.isChecked()) {
                    setAllEnabled(true);
                }
                break;
            case R.id.checkSa:
                if (saturday.isChecked()){
                    Toast.makeText(this, "Saturday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Saturday";
                    topic2="Rpi/SaturdayEnd";

                    setAllEnabled(false);
                    saturday.setEnabled(true);
                }
                else if (!saturday.isChecked()) {
                    setAllEnabled(true);
                }
                break;
            case R.id.checkSu:
                if (sunday.isChecked()){
                    Toast.makeText(this, "Sunday", Toast.LENGTH_SHORT).show();
                    topic="Rpi/Sunday";
                    topic2="Rpi/SundayEnd";

                    setAllEnabled(false);
                    sunday.setEnabled(true);
                }
                else if (!sunday.isChecked()) {
                    setAllEnabled(true);
                }
                break;
            default:
                break;
        }

       // if(!everyDay.isChecked() || !monday.isChecked() || !tuesday.isChecked() || !wednesday.isChecked()|| !thursday.isChecked() || !friday.isChecked() || !sunday.isChecked() || !saturday.isChecked()){
       //     topic= " ";
       //     Toast.makeText(this, "Choose a day", Toast.LENGTH_SHORT).show();
       // }
    }

    public void setAllChecked(boolean checked){

        monday.setChecked(checked);
        tuesday.setChecked(checked);
        wednesday.setChecked(checked);
        thursday.setChecked(checked);
        friday.setChecked(checked);
        saturday.setChecked(checked);
        sunday.setChecked(checked);

    }

    public void setAllEnabled(boolean enabled){

            everyDay.setEnabled(enabled);
            monday.setEnabled(enabled);
            tuesday.setEnabled(enabled);
            wednesday.setEnabled(enabled);
            thursday.setEnabled(enabled);
            friday.setEnabled(enabled);
            saturday.setEnabled(enabled);
            sunday.setEnabled(enabled);
    }

    public String convertDate(int input) {
        if (input >= 10) {
            return String.valueOf(input);
        } else {
            return "0" + String.valueOf(input);
        }
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        if (time==false){
            Button btStart = findViewById(R.id.button_start);
            String text =  convertDate(hourOfDay)+ ":" + convertDate(minute);
            btStart.setText(text);
            message= text;
            }

        else if(time){
            Button btEnd= findViewById(R.id.button_end);
            String text =  convertDate(hourOfDay)+ ":" + convertDate(minute);
            btEnd.setText(text);
            message2= text;
            }


    }




}
