package com.draw.painting.line.ui;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.draw.painting.line.R;
import com.draw.painting.line.databinding.ActivityMainBinding;
import com.draw.painting.line.manager.AppHelper;
import com.google.android.material.bottomnavigation.BottomNavigationView;

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

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    private List<Fragment> listFragment = new ArrayList<>();

    private String[] permissions;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppHelper.enableEdgeToEdge(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        AppHelper.setupWindowInsetsListener(binding.getRoot());
        AppHelper.setupBottomNavigationInsets(binding.bottomNavigation);

        // 初始化Fragment列表
        listFragment.add(CategoryFragment.newInstance());      // 0: Draw
        listFragment.add(SettingsFragment.newInstance());   // 1: Parents
        
        binding.viewPager2.setUserInputEnabled(false);
        binding.viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getItemCount() {
                return listFragment.size();
            }
        });
        
        initBottomNavigation();
    }



    private void initBottomNavigation() {
        binding.bottomNavigation.setOnItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();
                if (itemId == R.id.nav_draw) {
                    binding.viewPager2.setCurrentItem(0, false);
                    return true;
                } else if (itemId == R.id.nav_parents) {
                    binding.viewPager2.setCurrentItem(1, false);
                    return true;
                }
                return false;
            }
        });
        
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        binding.bottomNavigation.setSelectedItemId(R.id.nav_draw);
                        break;
                    case 1:
                        binding.bottomNavigation.setSelectedItemId(R.id.nav_parents);
                        break;
                }
            }
        });
    }



}

