package com.kfb.kfbv1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.kfb.kfbv1.db.DbManage;
import com.kfb.kfbv1.db.Urzadzenia;
import com.kfb.kfbv1.model.KfbModel;

import java.util.List;

public class ConfigActivity  extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        ImageButton btn = (ImageButton) findViewById(R.id.back);
        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onBack(v);
            }
        });
        Button b1 = (Button)findViewById(R.id.idSave);
        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onSave(v);
            }
        });
        TextView t1 = (TextView) findViewById(R.id.g1t);
        t1.setText(KfbModel.getData().getGates().getmGate().get(0).getmAdres());
        t1 = (TextView) findViewById(R.id.g2t);
        t1.setText(KfbModel.getData().getGates().getmGate().get(1).getmAdres());
        t1 = (TextView) findViewById(R.id.g3t);
        t1.setText(KfbModel.getData().getGates().getmGate().get(2).getmAdres());
        t1 = (TextView) findViewById(R.id.g4t);
        t1.setText(KfbModel.getData().getGates().getmGate().get(3).getmAdres());
        Switch s1 = (Switch) findViewById(R.id.g1s);
        s1.setChecked(KfbModel.getData().getGates().getmGate().get(0).ismActive());
        s1 = (Switch) findViewById(R.id.g2s);
        s1.setChecked(KfbModel.getData().getGates().getmGate().get(1).ismActive());
        s1 = (Switch) findViewById(R.id.g3s);
        s1.setChecked(KfbModel.getData().getGates().getmGate().get(2).ismActive());
        s1 = (Switch) findViewById(R.id.g4s);
        s1.setChecked(KfbModel.getData().getGates().getmGate().get(3).ismActive());
        DbManage dbRep = new DbManage(getApplicationContext());
        Urzadzenia urzdb = dbRep.getUrzadzenie(4);
        if(urzdb!=null) {
            s1 = (Switch) findViewById(R.id.t1s);
            s1.setChecked(urzdb.active);
            t1 = (TextView) findViewById(R.id.t1t);
            t1.setText(urzdb.adres);
        }
        urzdb = dbRep.getUrzadzenie(5);
        if(urzdb!=null) {
            s1 = (Switch) findViewById(R.id.t2s);
            s1.setChecked(urzdb.active);
            t1 = (TextView) findViewById(R.id.t2t);
            t1.setText(urzdb.adres);
        }
        urzdb = dbRep.getUrzadzenie(6);
        if(urzdb!=null) {
            s1 = (Switch) findViewById(R.id.t3s);
            s1.setChecked(urzdb.active);
            t1 = (TextView) findViewById(R.id.t3t);
            t1.setText(urzdb.adres);
        }
        urzdb = dbRep.getUrzadzenie(7);
        if(urzdb!=null) {
            s1 = (Switch) findViewById(R.id.t4s);
            s1.setChecked(urzdb.active);
            t1 = (TextView) findViewById(R.id.t4t);
            t1.setText(urzdb.adres);
        }
    }

    private void onBack(View v)
    {
        Intent intent = new Intent(this, GateActivity.class);
        startActivityForResult(intent,1);
        finish();
    }
    private void onSave(View v)
    {
        // aktualizuj baze
        DbManage dbRep = new DbManage(getApplicationContext());
        Urzadzenia urzdbe = new Urzadzenia();

        TextView t1 = (TextView) findViewById(R.id.g1t);
        String s =   t1.getText().toString();
        KfbModel.getData().getGates().getmGate().get(0).setmAdres(s);
        Switch s1 = (Switch) findViewById(R.id.g1s);
        KfbModel.getData().getGates().getmGate().get(0).setmActive(s1.isChecked());

        urzdbe.active = s1.isChecked();
        urzdbe.adres = t1.getText().toString();
        urzdbe.ID =  KfbModel.getData().getGates().getmGate().get(0).getId();
        dbRep.updateUrzazenie(urzdbe);
        urzdbe = new Urzadzenia();
        t1 = (TextView) findViewById(R.id.g2t);
        KfbModel.getData().getGates().getmGate().get(1).setmAdres( t1.getText().toString());
        s1 = (Switch) findViewById(R.id.g2s);
        KfbModel.getData().getGates().getmGate().get(1).setmActive(s1.isChecked());

        urzdbe.active = s1.isChecked();
        urzdbe.adres = t1.getText().toString();
        urzdbe.ID =  KfbModel.getData().getGates().getmGate().get(1).getId();
        dbRep.updateUrzazenie(urzdbe);
        urzdbe = new Urzadzenia();
        t1 = (TextView) findViewById(R.id.g3t);
        KfbModel.getData().getGates().getmGate().get(2).setmAdres(t1.getText().toString());
        s1 = (Switch) findViewById(R.id.g3s);
        KfbModel.getData().getGates().getmGate().get(2).setmActive(s1.isChecked());

        urzdbe.active = s1.isChecked();
        urzdbe.adres = t1.getText().toString();
        urzdbe.ID =  KfbModel.getData().getGates().getmGate().get(2).getId();
        dbRep.updateUrzazenie(urzdbe);
        urzdbe = new Urzadzenia();
        t1 = (TextView) findViewById(R.id.g4t);
        KfbModel.getData().getGates().getmGate().get(3).setmAdres( t1.getText().toString());
        s1 = (Switch) findViewById(R.id.g4s);
        KfbModel.getData().getGates().getmGate().get(3).setmActive(s1.isChecked());

        urzdbe.active = s1.isChecked();
        urzdbe.adres = t1.getText().toString();
        urzdbe.ID =  KfbModel.getData().getGates().getmGate().get(3).getId();
        dbRep.updateUrzazenie(urzdbe);
        t1 = (TextView) findViewById(R.id.t1t);
        s1 = (Switch) findViewById(R.id.t1s);

        Urzadzenia urzdb = dbRep.getUrzadzenie(4);
        if(urzdb==null)
        {
            urzdb = new Urzadzenia();
            urzdb.ID = 4;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.insertUrzazenie(urzdb);
        }
        else
        {
            urzdb.ID = 4;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.updateUrzazenie(urzdb);
        }
        t1 = (TextView) findViewById(R.id.t2t);
        s1 = (Switch) findViewById(R.id.t2s);

        urzdb = dbRep.getUrzadzenie(5);
        if(urzdb==null)
        {
            urzdb = new Urzadzenia();
            urzdb.ID = 5;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.insertUrzazenie(urzdb);
        }
        else
        {
            urzdb.ID = 5;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.updateUrzazenie(urzdb);
        }
        t1 = (TextView) findViewById(R.id.t3t);
        s1 = (Switch) findViewById(R.id.t3s);

        urzdb = dbRep.getUrzadzenie(6);
        if(urzdb==null)
        {
            urzdb = new Urzadzenia();
            urzdb.ID = 6;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.insertUrzazenie(urzdb);
        }
        else
        {
            urzdb.ID = 6;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.updateUrzazenie(urzdb);
        }
        t1 = (TextView) findViewById(R.id.t4t);
        s1 = (Switch) findViewById(R.id.t4s);

        urzdb = dbRep.getUrzadzenie(7);
        if(urzdb==null)
        {
            urzdb = new Urzadzenia();
            urzdb.ID = 7;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.insertUrzazenie(urzdb);
        }
        else
        {
            urzdb.ID = 7;
            urzdb.adres = t1.getText().toString();
            urzdb.active = s1.isChecked();
            dbRep.updateUrzazenie(urzdb);
        }
        this.onBack(v);
    }
}
