package com.app.input.personalised.board.boartactivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.ad.tradpluslibrary.TPAdManager;
import com.app.input.personalised.board.R;
import com.app.input.personalised.board.databinding.ActivityMainTabBinding;
import com.app.input.personalised.board.helpers.FontTextView;
import com.app.input.personalised.board.boartfragment.CategoryFragment;
import com.app.input.personalised.board.boartfragment.MyFragment;
import com.app.input.personalised.board.boartfragment.HomeFragment;
import com.google.android.material.tabs.TabLayout;

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

public class MainTabActivity extends AppCompatActivity {

    private ActivityMainTabBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMainTabBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(vb.getRoot());
        com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 延迟设置状态栏，确保覆盖MaterialComponents主题可能的自动设置
        findViewById(R.id.main).post(() -> {
            com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(this);
        });

        init();

    }

    @Override
    protected void onResume() {
        super.onResume();
        TPAdManager.INSTANCE.loadAllAd(this);
        // 确保状态栏始终为透明
        com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            // 窗口获得焦点时，确保状态栏为透明
            com.app.input.personalised.board.helpers.StatusBarHelper.INSTANCE.setTransparentStatusBar(this);
        }
    }

    private void init() {
       List<Fragment> listFragment = new ArrayList<>();
        listFragment.add(HomeFragment.newInstance());
        listFragment.add(CategoryFragment.newInstance());
        listFragment.add(MyFragment.newInstance());

        for (int i = 0; i < listFragment.size(); i++) {
            TabLayout.Tab tab = vb.tabLayout.newTab();

            View inflate = LayoutInflater.from(this).inflate(R.layout.tab_custom_view, null, false);
            ImageView icon = inflate.findViewById(R.id.im_icon);
            FontTextView textView = inflate.findViewById(R.id.textView);
            if (i == 0) {
                textView.setSelected(true);
                textView.setText(getString(R.string.tab_home));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home_selecto));
            } else if(i == 1){
                textView.setSelected(false);
                textView.setText(getString(R.string.tab_category));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_category_selecto));
            } else if(i == 2){
                textView.setSelected(false);
                textView.setText(getString(R.string.tab_favorite));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_favorite_selecto));
            }
            tab.setCustomView(inflate);
            vb.tabLayout.addTab(tab);
        }


        vb.viewpager.setUserInputEnabled(false);
        // 减少预加载页面数量，只预加载相邻的1个页面，减少初始化开销
        vb.viewpager.setOffscreenPageLimit(1);
        vb.viewpager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

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


    private void bing() {
        vb.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                icon.setSelected(true);
                vb.viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                icon.setSelected(false);
            }

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

            }
        });
        vb.viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Objects.requireNonNull(vb.tabLayout.getTabAt(position)).select();
            }
        });
    }
}