package w.soulofpluto.posstock;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/*
 Create by Ramesh Babu
 */

public class DashBoardActivity extends AppCompatActivity implements View.OnClickListener {
    Toolbar dashboard_toolbar;
    Button stock_audit,rack_qty,searchby_barcode, transfer;
    TextView version;
    PreferenceManager preferenceManager;
    @Override
    protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_dashboard);
      stock_audit       = findViewById(R.id.stock_audit);
      rack_qty          = findViewById(R.id.rack_qty);
      searchby_barcode  = findViewById(R.id.searchby_barcode);
      dashboard_toolbar = findViewById(R.id.dashboard_toolbar);
      version           = findViewById(R.id.version);
      transfer          = findViewById(R.id.transfer);
      preferenceManager = new PreferenceManager(this);
      setSupportActionBar(dashboard_toolbar);
      getSupportActionBar().setTitle(getResources().getString(R.string.dashboard));
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      stock_audit.setOnClickListener(this);
      rack_qty.setOnClickListener(this);
      searchby_barcode.setOnClickListener(this);
      transfer.setOnClickListener(this);
      version.setText(Html.fromHtml(Constants.Ver_Text +Constants.VERSION));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
      getMenuInflater().inflate(R.menu.menu, menu);
      return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item){
      switch(item.getItemId()){
          case R.id.dashboard_logout:
           if(Constants.isNetworkAvailable(DashBoardActivity.this)){
             Logout(Constants.Logout+preferenceManager.getUserId());
             Constants.ProgressDialogShow(DashBoardActivity.this);
           }else{
             Constants.getToast(DashBoardActivity.this,getResources().getString(R.string.connection));
           }
          break;

          case android.R.id.home:
            finish();
          break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void Logout(String Url){
        final OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(240, TimeUnit.SECONDS);
        client.setReadTimeout(240,TimeUnit.SECONDS);
        client.setWriteTimeout(240,TimeUnit.SECONDS);
        final Request request = new Request.Builder()
                .url(Url)
                .get()
                .addHeader("Content-Type", "application/json")
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
              Constants.ProgressDialogDismiss();
            }
            @Override
            public void onResponse(Response response) throws IOException{
             Constants.ProgressDialogDismiss();
             if(response.isSuccessful()){
                 String res = response.body().string();
                 Log.e("Dashboard Logout ", res);
                 try{
                     JSONObject jsonObject = new JSONObject(res);
                     if(jsonObject.has("success")){
                        if(jsonObject.getInt("success") == 1){
                            preferenceManager.setUserId("");
                            preferenceManager.setLogged(false);
                            preferenceManager.setCustToken("");
                            preferenceManager.setStoreId("");
                            Login.logout(DashBoardActivity.this);
                        }else{
                            Constants.getToast(DashBoardActivity.this,jsonObject.getString("msg"));
                        }
                     }
                 }catch(JSONException e){
                   e.printStackTrace();
                 }catch (NullPointerException np){
                   np.printStackTrace();
                 }
             }
           }
        });
    }

    @Override
    public void onClick(View view){
       switch(view.getId()){
           case R.id.stock_audit:
             Intent in_main = new Intent(this,MainActivity.class);
             startActivity(in_main);
            break;
           case R.id.rack_qty:
             Intent in_rack = new Intent(this,RackWiseQty.class);
             startActivity(in_rack);
            break;
           case R.id.searchby_barcode:
             Intent in_search = new Intent(this, SearchByBarcode.class);
             startActivity(in_search);
            break;
       }
    }
}
