package com.kfb.kfbv1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
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.ProgressBar;
import android.widget.TextView;

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;


public class MainActivity extends AppCompatActivity {
    private WifiApManager wifiApManager;
    private ProgressBar progressBar;
    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1) {
                TextView outtt = (TextView) findViewById(R.id.editText4);
              //  if(KfbModel.getData().error==false) {
              //      outtt.setText(KfbModel.getData().getGates().getmGate().get(0).getmMaxT()+
              //              "status=" + KfbModel.getData().getGates().getmGate().get(0).getmStatus() );
              //  }
               // else{
                    outtt.setText(KfbModel.getData().tekser);
              //  }
                KfbModel.getData().progresBar.dismiss();
            }
            super.handleMessage(msg);
        }
    };
    private KfbTask tr = new KfbTask();
    private Thread T1 = null;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Button btn1 = (Button) findViewById(R.id.button);
        btn1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                try {
                    onSendClik(v);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Button btn = (Button) findViewById(R.id.login);
        btn.setOnClickListener(new View.OnClickListener() {

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

       wifiApManager = new WifiApManager(this);
       // force to show the settings page for demonstration purpose of this method
      // wifiApManager.showWritePermissionSettings(true);
      //WifiConfiguration con = new WifiConfiguration();
       //con.SSID = "KFB_SaFE";
       //wifiApManager.setWifiApEnabled(con, true);
    }
    private void onLoginClik(View v) {
        Intent i = new Intent(getApplicationContext(),GateActivity.class);
        startActivity(i);

    }
    private void onSendClik(View v) throws InterruptedException {
        KfbModel.getData().progresBar = new KfbProgrsBar(this);
        KfbModel.getData().progresBar.show();
        TextView url = (TextView)findViewById(R.id.editText);
        KfbModel.getData().tekser = null;
        KfbModel.getData().handler = handler;
        KfbModel.getData().url = url.getText().toString();

        KfbGate g1 = new KfbGate();
        g1.setmName("Test1");
    //    KfbModel.getData().getGates().AddGate(g1);

        T1 = new Thread(tr);
        T1.start();
    }
    @Override
    protected void onResume() {
        super.onResume();

        wifiApManager.showWritePermissionSettings(false);
    }

}
