package com.kfb.kfbv1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kfb.kfbv1.db.DbManage;
import com.kfb.kfbv1.db.Urzadzenia;
import com.kfb.kfbv1.db.User;
import com.kfb.kfbv1.hotspot.WifiApManager;
import com.kfb.kfbv1.model.KfbGate;
import com.kfb.kfbv1.model.KfbGates;
import com.kfb.kfbv1.model.KfbHttpRequest;
import com.kfb.kfbv1.model.KfbModel;
import com.kfb.kfbv1.model.KfbTask;

import java.util.List;


public class MainActivity extends AppCompatActivity {
    private WifiApManager wifiApManager;
    private ProgressBar progressBar;

    private KfbTask tr = new KfbTask();
    private Thread T1 = null;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
               WindowManager.LayoutParams.FLAG_FULLSCREEN);
       View decorView = getWindow().getDecorView();
       int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
       decorView.setSystemUiVisibility(uiOptions);


       ImageButton btn1 = (ImageButton) findViewById(R.id.idbConfig);
       btn1.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
               onSendClik(v);
           }
       });
       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");
               }
               if(i<4) {
                   lGate.setmActive(urzdb.get(i).active);
                   KfbModel.getData().getGates().AddGate(lGate);
               }
               else
               {
                   lGate.setmActive(urzdb.get(i).active);
                   KfbModel.getData().getTabletss().AddTablet(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);
       }
       dbRep.close();
       KfbModel.getData().s1 = true;
       Thread t1 = new Thread(new Runnable() {
           @Override
           public void run() {
               try {

                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               if(KfbModel.getData().s1==true) {
                   Intent i = new Intent(getApplicationContext(), GateActivity.class);
                   startActivity(i);
               }
           }
       });
       t1.start();

   }

    private void onSendClik(View v)   {
        KfbModel.getData().s1 = false;
        Intent i = new Intent(getApplicationContext(),PinActivity.class);
        startActivity(i);
    }
    @Override
    protected void onResume() {
        super.onResume();

      //  wifiApManager.showWritePermissionSettings(false);
    }

}
