package com.app.wallpaper.myact;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;

import android.os.Build;
import android.os.Bundle;

import android.view.MenuItem;


import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.app.wallpaper.R;
import com.app.wallpaper.databinding.ActivityMainBinding;
import com.app.wallpaper.myada.MainFragmentAddapter;
import com.app.wallpaper.myfra.HomeFragment;
import com.app.wallpaper.myfra.MyfavFragment;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private final List<Fragment> mfragmentList = new ArrayList<>();
    private static final int STORAGE_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        binding.getRoot();
        setContentView(binding.getRoot());
        checkPermission(this, STORAGE_PERMISSION_CODE);
        mfragmentList.add(new HomeFragment());
        mfragmentList.add(new MyfavFragment());
        MainFragmentAddapter myFragmentAdapter = new MainFragmentAddapter(MainActivity.this, mfragmentList);
        binding.viewPager.setAdapter(myFragmentAdapter);
        new TabLayoutMediator(binding.tabLayout, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                if (position == 0) {
                    tab.setIcon(R.drawable.ic_home);
                } else {
                    tab.setIcon(R.drawable.frame_3_);
                }
            }
        }).attach();
    }

    private boolean checkPermission(Activity mActivity, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return true;
        } else {
            if (mActivity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        mActivity,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        requestCode
                );
                return false;
            } else {
                return true;
            }
        }
    }
}