package com.kfb.kfbv1;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kfb.kfbv1.dummy.DummyContent;

import com.kfb.kfbv1.model.KfbGate;
import com.kfb.kfbv1.model.KfbHttpRequest;
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 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 = ld1.get(msg.arg1);
                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();
                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);

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


        listAdapter = new MyItemRecyclerViewAdapter( ld1,null);
        recycler.setAdapter(listAdapter);

        KfbModel.getData().sendHttp=true;
        KfbGate lGate = new KfbGate();
        lGate.setmAdres("http://192.168.13.15");
      //  lGate.setmAdres("http://p.sorveno.com/pp.html");
        lGate.setmName("Gate 1");
        KfbModel.getData().getGates().AddGate(lGate);
        lGate = new KfbGate();
        lGate.setmAdres("http://192.168.13.16");
        lGate.setmName("Gate 2");
        KfbModel.getData().getGates().AddGate(lGate);
        for (int i =0; i<KfbModel.getData().getGates().getmSize();i++) {
             DummyContent.DummyItem d1 = new DummyContent.DummyItem(String.valueOf(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());
            ld1.add(d1);
        }
        KfbModel.getData().handler = handler;
        KfbModel.getData().setmAdapter(listAdapter);
        this.StartMes();
        listAdapter.notifyDataSetChanged();
    }

    private void StartMes(){
        KfbModel.getData().sendHttp= true;
        KfbTask tr = new KfbTask();
        Thread T1 = new Thread(tr);
        T1.start();
    }


}