package com.wall.turing.wallpapers.accctivities;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.tradplus.ads.base.bean.TPAdInfo;
import com.wall.turing.wallpapers.R;
import com.wall.turing.wallpapers.ad.AdsInsUtil;
import com.wall.turing.wallpapers.ad.LoadListener;
import com.wall.turing.wallpapers.databinding.TuringMainBinding;
import com.wall.turing.wallpapers.databinding.TabAdapterBinding;
import com.wall.turing.wallpapers.turingfragments.HomeContainerPage;
import com.wall.turing.wallpapers.turingfragments.LikePage;
import com.wall.turing.wallpapers.turingfragments.SettingsPage;
import com.wall.turing.wallpapers.uutils.AppUtils;

import org.jetbrains.annotations.NotNull;

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

public class PageMainActivity extends AppCompatActivity {


    private TuringMainBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = TuringMainBinding.inflate(getLayoutInflater());
        AppUtils.initFull(this, false);
        setContentView(vb.getRoot());
        loadAd();
        // 设置主ViewPager（首页、收藏、设置）
        setupMainViewPager();

        setupBottomNavigation();
    }
    
    private void setupMainViewPager() {
        // 主页面：首页容器、收藏、设置
        List<Fragment> mainList = new ArrayList<>();
        mainList.add(HomeContainerPage.newInstance());
        mainList.add(LikePage.newInstance());
        mainList.add(SettingsPage.newInstance());
        
        vb.mainViewpager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mainList.get(position);
            }

            @Override
            public int getItemCount() {
                return mainList.size();
            }
        });
        
        vb.mainViewpager.setUserInputEnabled(false); // 禁止左右滑动
    }
    
    private void setupBottomNavigation() {
        int[] ints = {R.drawable.select_home1, R.drawable.select_tab_like1, R.drawable.select_settings};
        
        for (int i = 0; i < 3; i++) {
            TabLayout.Tab tab = vb.tab.newTab();
            TabAdapterBinding tabVb = TabAdapterBinding.inflate(getLayoutInflater());
            tabVb.imageIcon.setImageDrawable(ContextCompat.getDrawable(this, ints[i]));
            tab.setCustomView(tabVb.getRoot());
            if (i == 0) {
                tabVb.imageIndicator.setVisibility(View.VISIBLE);
            } else {
                tabVb.imageIndicator.setVisibility(View.GONE);
            }
            vb.tab.addTab(tab);
        }
        
        vb.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                vb.mainViewpager.setCurrentItem(position);
                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.INVISIBLE);
            }

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


    private void loadAd() {
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_ONE, new LoadListener() {
            @Override
            public void loaded(@NotNull TPAdInfo ad) {

            }

            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new LoadListener() {
            @Override
            public void loaded(@NotNull TPAdInfo ad) {

            }

            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}