package com.wall.turing.wallpapers.turingfragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.wall.turing.wallpapers.R;
import com.wall.turing.wallpapers.accctivities.TuringSearchActivity;
import com.wall.turing.wallpapers.databinding.HomeContainerPageBinding;

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

public class HomeContainerPage extends Fragment {

    private HomeContainerPageBinding vb;

    public static HomeContainerPage newInstance() {
        return new HomeContainerPage();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        vb = HomeContainerPageBinding.inflate(inflater, container, false);
        return vb.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 添加状态栏高度适配
        com.wall.turing.wallpapers.uutils.AppUtils.addStatusBarPadding(vb.topTitleBar, requireContext());
        
        setupContentViewPager();
        
        // 搜索按钮
        vb.imSearch.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), TuringSearchActivity.class);
            startActivity(intent);
        });
    }
    
    private void setupContentViewPager() {
        // 内容页面：推荐和分类
        List<Fragment> contentList = new ArrayList<>();
        contentList.add(RecommendPage.newInstance());
        contentList.add(MainPage.newInstance(0));
        
        vb.contentViewpager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return contentList.get(position);
            }

            @Override
            public int getItemCount() {
                return contentList.size();
            }
        });
        
        // 设置顶部Tab
        vb.topTab.addTab(vb.topTab.newTab().setText(getString(R.string.recommend)));
        vb.topTab.addTab(vb.topTab.newTab().setText(getString(R.string.category)));
        
        // 顶部Tab和内容ViewPager联动
        vb.topTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vb.contentViewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        
        vb.contentViewpager.registerOnPageChangeCallback(new androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                vb.topTab.selectTab(vb.topTab.getTabAt(position));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        vb = null;
    }
}


