package com.example.java.sample;

import static com.example.java.sample.MainFragment.REQUEST_LOCATION_SETTING_RESOLUTION;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.java.sample.databinding.ActivityMainBinding;
import com.seikoinstruments.sdk.thermalprinter.PrinterManager;
import android.content.pm.ActivityInfo;

import java.util.Objects;

/**
 * This class represents the main activity of the application.
 */
public class MainActivity extends AppCompatActivity {
    // Variables declaration
    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;
    public int mPrinterType = PrinterManager.PRINTER_TYPE_BLUETOOTH;
    public final static String REQUEST_KEY_SEARCH = "request_key_search";
    public final static String REQUEST_KEY_SETTINGS = "request_key_settings";

    public final static String BUNDLE_KEY_SOCKET_KEEPING_TIME = "key_socketKeepingTime";
    public final static String BUNDLE_KEY_SEND_TIMEOUT = "key_sendTimeout";
    public final static String BUNDLE_KEY_RECEIVE_TIMEOUT = "key_receiveTimeout";
    public final static String BUNDLE_KEY_CODE_PAGE = "key_codePage";
    public final static String BUNDLE_KEY_INTERNATION_CHAR = "key_internationalChar";
    public final static String BUNDLE_KEY_SECURE_CONNECTION = "key_secureConnection";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(!isTablet(getApplicationContext()) ){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        // Inflate the layout and set up the toolbar
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);

        //Support edge to edge
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(binding.getRoot().getRootView(), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // Set up navigation controller and app bar configuration
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    }

    private boolean isTablet(Context context) {
        return context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
    }

    @Override
    public boolean onSupportNavigateUp() {
        // Handle navigation up action
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Handle result of location setting resolution
        if (requestCode == REQUEST_LOCATION_SETTING_RESOLUTION) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // Navigate to DeviceFragment upon successful resolution
                    NavHostFragment.findNavController(Objects.requireNonNull(getSupportFragmentManager().getPrimaryNavigationFragment()))
                            .navigate(R.id.action_MainFragment_to_DeviceFragment);
                    break;
                case Activity.RESULT_CANCELED:
                default:
                    break;
            }
        }
    }

}