package com.haven.wallpapers.activity;

import android.Manifest;
import android.annotation.SuppressLint;
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.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.haven.wallpapers.R;
import com.haven.wallpapers.Utils.ReadFile;
import com.haven.wallpapers.fragment.FavoriteFragment;
import com.haven.wallpapers.fragment.HomeFragment;
import com.haven.wallpapers.fragment.ImportFragment;
import com.haven.wallpapers.adapter.viewpagerAdapter;
import com.haven.wallpapers.topon.AdManager;

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);
        AdManager.loadAllAd();
        navigationView = findViewById(R.id.bottomNavigation);
        viewPager = findViewById(R.id.viewPager);
        ReadFile.initFull(MainActivity.this,false);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ImportFragment());
        fragments.add(new FavoriteFragment());
        //若跳转多个界面添加多个fragment
        viewpagerAdapter adapter = new viewpagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
        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;
            }
        }
    }


}