package com.cscodetech.movers.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.ActivityHomeBinding;
import com.cscodetech.movers.fragment.HomeFragment;

public class HomeActivity extends BaseActivity {

    private ActivityHomeBinding binding;
    HomeActivity activity;
    public HomeActivity getInstance(){
        if(activity == null){
            activity = new HomeActivity();
        }
        return activity;
    }
    @androidx.annotation.RequiresApi(api = android.os.Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        activity = this;
        androidx.core.app.ActivityCompat.requestPermissions(this,
                new String[] {
                        android.Manifest.permission.READ_EXTERNAL_STORAGE,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.CAMERA,android.Manifest.permission.CALL_PHONE
                }, 1010);
        binding.bottomNavigation.setOnItemSelectedListener(item -> {
            // Handle navigation item selection
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_home) {
                openFragment(new com.cscodetech.movers.fragment.HomeFragment());
            } else if (itemId == R.id.navigation_sms) {
                item.setCheckable(false);
                startActivity(new Intent(HomeActivity.this, com.cscodetech.movers.ui.postload.MyPostLoadActivity.class));
            } else if (itemId == R.id.navigation_notifications) {
                item.setCheckable(false);
                startActivity(new Intent(HomeActivity.this, com.cscodetech.movers.ui.findlorry.MyBookLoadActivity.class));
            } else if (itemId == R.id.navigation_notifications2) {
                openFragment(new com.cscodetech.movers.fragment.ProfileFragment());
            }
            return true;
        });
        openFragment(new com.cscodetech.movers.fragment.HomeFragment());
    }

    public void openFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
    public void profileMenuClick() {
        Log.e("click","99999");
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
        if (fragment instanceof HomeFragment && fragment.isVisible()) {
            //DO STUF
            Log.e("dada","active");
        }
        else {
            finish();
            //Whatever
            Log.e("dada","Inactive");

        }
    }






}