package com.sliit.smartserviceassistant;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.sql.Connection;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;


public class Loging extends AppCompatActivity {

    //
   // EditText textInputEditTextUsername,textInputEditTextPassword;
    Button buttonLogin;
    TextView textViewLoging;
    ProgressBar progressBar;
    Intent i;
    Connection conexion=null;
    TextView textUsername,textPassword;
    String Confirmusername;
    String Confirmpassword;


    private OkHttpClient mclient;

    private String strURL;
    //="http://192.168.8.103:8000/api/login?username=hasith&user_pass=1234";//GET

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //for changing status bar icon colors
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        }
        setContentView(R.layout.activity_loging);

        textUsername = (TextView) findViewById(R.id.editTextusername);
        textPassword = (TextView) findViewById(R.id.editTextPassword);


        Button sendButton = (Button) findViewById(R.id.cirLoginButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                     Confirmusername=textUsername.getText().toString();
                   Confirmpassword=textPassword.getText().toString();


                strURL ="http://192.168.1.5:8000/api/login?username="+Confirmusername+"&user_pass="+Confirmpassword;

                mclient=new OkHttpClient();

                Request request= new Request.Builder().url(strURL).build();



                mclient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        if(response.isSuccessful())
                        {

                            String strJson=response.body().string();
                            System.out.println(strJson);
                            if(strJson.equals("ok")==true){
                                //Toast.makeText(Loging.this, "You form is sucessfull", Toast.LENGTH_SHORT).show();

                                                Intent k = new Intent(Loging.this, MainActivity.class);
                                                 startActivity(k);
                                                 finish();

                            }

                        }else{
                            System.out.println("fail");
                        }
                    }
                });

            }
        });





}


//
    public void onLoginClick(View View){

        startActivity(new Intent(this,SignUp.class));
        overridePendingTransition(R.anim.slide_in_right,R.anim.stay);


    }


}
