package com.kfb.kfbv1;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;


import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kfb.kfbv1.db.DbManage;
import com.kfb.kfbv1.db.Urzadzenia;
import com.kfb.kfbv1.dummy.DummyContent;

import com.kfb.kfbv1.model.KfbGate;
import com.kfb.kfbv1.model.KfbModel;
import com.kfb.kfbv1.model.KfbTask;
import java.util.ArrayList;
import java.util.List;

public class GateActivity  extends AppCompatActivity
{
    private Toolbar toolbar;
    private MyItemRecyclerViewAdapter listAdapter;
    private List<DummyContent.DummyItem> ld1 = new ArrayList<DummyContent.DummyItem>() ;
    private RecyclerView recycler;
    private RecyclerView.LayoutManager layoutManager;
    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==2) {
                DummyContent.DummyItem dr=null;
                for(int i = 0; i<ld1.size();i++)
                {
                    dr = ld1.get(msg.arg1);
                    if(dr.id== msg.arg1)
                    {
                        break;
                    }
                }
                if(dr!=null) {
                    dr.Temperatura = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMaxT();
                    dr.State = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmStatus();
                    dr.Error = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmError();
                    dr.imgHis = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHist();
                    float t=0;
                    if (dr.Temperatura != null) {
                        t = Float.parseFloat(dr.Temperatura);
                    }
                    if (t > 38) {
                         dr.Nok = "NOK";
                    } else {
                        dr.Nok = "OK";
                    }
                    listAdapter.notifyDataSetChanged();
                }
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gate);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        ImageButton bConf = (ImageButton)findViewById(R.id.idConfig);
        bConf.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onConfigClik(v);
            }
        });

        recycler = findViewById(R.id.recview);
        recycler.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(layoutManager);



        listAdapter = new MyItemRecyclerViewAdapter( ld1,null);
        recycler.setAdapter(listAdapter);
        DbManage dbRep = new DbManage(getApplicationContext());
        List<Urzadzenia> urzdb = dbRep.getUrzadzeniaAll();
            if(urzdb!=null) {
                KfbModel.getData().clear();
                for(int i=0;i<urzdb.size();i++){

                    KfbGate lGate = new KfbGate();
                    lGate.setId(urzdb.get(i).ID);
                    lGate.setmAdres(urzdb.get(i).adres);
                    if(i==0) {
                        lGate.setmName("Gate 1");
                    }
                    if(i==1) {
                        lGate.setmName("Gate 2");
                    }
                    if(i==2) {
                        lGate.setmName("Gate 3");
                    }
                    if(i==3) {
                        lGate.setmName("Gate 4");
                    }
                    lGate.setmActive(urzdb.get(i).active);
                    KfbModel.getData().getGates().AddGate(lGate);
                }
            }
                if (KfbModel.getData().getGates().getmSize() == 0) {
                    KfbModel.getData().clear();
                    KfbModel.getData().sendHttp = true;
                    KfbGate lGate = new KfbGate();
                    lGate.setId(0);
                    lGate.setmAdres("http://192.168.1.15:6505");
                    // lGate.setmAdres("http://51.83.130.248:3000");
                    lGate.setmName("Gate 1");
                    lGate.setmActive(true);
                    KfbModel.getData().getGates().AddGate(lGate);
                    Urzadzenia urzdbe = new Urzadzenia();

                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);

                    lGate = new KfbGate();
                    lGate.setmAdres("http://192.168.1.16:6505");
                    //lGate.setmAdres("http://p.sorveno.com/pp.html");
                    lGate.setmName("Gate 2");
                    lGate.setId(1);
                    lGate.setmActive(true);
                    KfbModel.getData().getGates().AddGate(lGate);
                    urzdbe = new Urzadzenia();
                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);
                    lGate = new KfbGate();
                    lGate.setmAdres("http://192.168.1.17:6505");
                    //lGate.setmAdres("http://p.sorveno.com/pp.html");
                    lGate.setmName("Gate 3");
                    lGate.setId(2);
                    lGate.setmActive(true);
                    KfbModel.getData().getGates().AddGate(lGate);
                    urzdbe = new Urzadzenia();
                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);
                    lGate = new KfbGate();
                    lGate.setmAdres("http://192.168.1.18:6505");
                    //lGate.setmAdres("http://p.sorveno.com/pp.html");
                    lGate.setmName("Gate 4");
                    lGate.setId(3);
                    lGate.setmActive(true);
                    urzdbe = new Urzadzenia();
                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);
                    KfbModel.getData().getGates().AddGate(lGate);
                }
            ld1.clear();
            for (int i = 0; i < KfbModel.getData().getGates().getmSize(); i++) {
                DummyContent.DummyItem d1 = new DummyContent.DummyItem(i, KfbModel.getData().getGates().getmGate().get(i).getmName(), "ON", KfbModel.getData().getGates().getmGate().get(i).getmError(), KfbModel.getData().getGates().getmGate().get(i).getmStatus(), "", "", "", KfbModel.getData().getGates().getmGate().get(i).getmMaxT());
                 if( KfbModel.getData().getGates().getmGate().get(i).ismActive()) {
                     ld1.add(d1);
                 }
            }
            KfbModel.getData().handler = handler;
            KfbModel.getData().setmAdapter(listAdapter);
            this.StartMes();
            listAdapter.notifyDataSetChanged();
    }
private void onConfigClik(View v)
{
    Intent i = new Intent(getApplicationContext(),ConfigActivity.class);
    startActivity(i);
}
    private void StartMes(){
        KfbModel.getData().sendHttp= true;
        KfbTask tr = new KfbTask();
        Thread T1 = new Thread(tr);
        T1.start();
    }
    @Override
    protected void onStop() {
        super.onStop();
        KfbModel.getData().sendHttp=false;
    }
}