package com.lh.wallpaper2;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
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.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.lh.wallpaper2.fragment.FavoriteFragment;
import com.lh.wallpaper2.fragment.HomeFragment;
import com.lh.wallpaper2.fragment.ImportFragment;
import com.lh.wallpaper2.fragment.SettingFragment;
import com.lh.wallpaper2.adapter.MyFragmentStateAdapter;

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

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView navigationView;
    private ViewPager viewPager;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        EdgeToEdge.enable(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigationView = findViewById(R.id.bottomNavigation);
        viewPager = findViewById(R.id.viewPager);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ImportFragment());
        fragments.add(new FavoriteFragment());
        //若跳转多个界面添加多个fragment
        MyFragmentStateAdapter adapter = new MyFragmentStateAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        navigationView.setSelectedItemId(R.id.navigation_home);
      //  getNavigationBarHeight(this);
        navigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.navigation_home) {
                    viewPager.setCurrentItem(0);
                } if (item.getItemId() == R.id.navigation_import){
                    viewPager.setCurrentItem(1);
                } if (item.getItemId() == R.id.navigation_favorite) {
                    viewPager.setCurrentItem(2);
                }
                return true;
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        navigationView.setSelectedItemId(R.id.navigation_home);
                        break;
                    case 1:
                        navigationView.setSelectedItemId(R.id.navigation_import);
                        break;
                    case 2:
                        navigationView.setSelectedItemId(R.id.navigation_favorite);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        checkPermission(this,1);
    }
    private boolean checkPermission(Activity mActivity, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return true;
        } else {
            if (mActivity.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        mActivity,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        requestCode
                );
                return false;
            } else {
                return true;
            }
        }
    }
    /*public int getNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("bottomNavigation", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }*/

}