package com.cake.draw.painting.activity;

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

import com.ad.tradpluslibrary.TPAdManager;
import com.cake.draw.painting.R;
import com.cake.draw.painting.databinding.ActivityHomeCakeBinding;
import com.cake.draw.painting.fragment.HomeCakeFragment;
import com.cake.draw.painting.fragment.SettingCakeFragment;
import com.google.android.material.tabs.TabLayout;

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

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;

public class HomeCakeActivity extends AppCompatActivity {

    private ActivityHomeCakeBinding binding;
    private List<Fragment> listFragment = new ArrayList<>();
    private String[] permissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        binding = ActivityHomeCakeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        View rootView = binding.getRoot();
        if (rootView != null) {
            ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
                Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                return insets;
            });
        }

        binding.tab.setSelectedTabIndicatorHeight(0); // 设置指示条高度为0
        binding.tab.setSelectedTabIndicator(null);    // 清空指示条样式
        binding.tab.setTabIndicatorFullWidth(false); // 确保指示条完全不显示（兜底）

        listFragment.add(HomeCakeFragment.newInstance());
        listFragment.add(SettingCakeFragment.newInstance());
        onTabInit();
        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();
            }
        });
        bingTabVp();
        TabLayout.Tab firstTab = binding.tab.getTabAt(0);
        if (firstTab != null) {
            View customView = firstTab.getCustomView();
            if (customView != null) {
                ImageView icon = customView.findViewById(R.id.im_icon);
                TextView tv = customView.findViewById(R.id.textView);
                icon.setSelected(true);
                tv.setSelected(true);
            }
        }
    }

    private void bingTabVp() {
        binding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View tabView = tab.getCustomView();
                if (tabView != null) {
                    ImageView ivIcon = tabView.findViewById(R.id.im_icon);
                    TextView tvText = tabView.findViewById(R.id.textView);
                    ivIcon.setSelected(true);
                    tvText.setSelected(true);
                }
                binding.viewPager2.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View tabView = tab.getCustomView();
                if (tabView != null) {
                    ImageView ivIcon = tabView.findViewById(R.id.im_icon);
                    TextView tvText = tabView.findViewById(R.id.textView);
                    ivIcon.setSelected(false);
                    tvText.setSelected(false);
                }
            }

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

        // ViewPager2联动Tab
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.tab.getTabAt(position).select();
            }
        });
    }

    private void onTabInit() {
        for (int i = 0; i < listFragment.size(); i++) {
            TabLayout.Tab tab = binding.tab.newTab();
            View inflate = LayoutInflater.from(this).inflate(R.layout.item_tab_selector_cake, null, false);
            ImageView icon = inflate.findViewById(R.id.im_icon);
            TextView textView = inflate.findViewById(R.id.textView);

            if (i == 0) {
                textView.setText(getString(R.string.tab_draw));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_tab_home_cake));
                icon.setSelected(true);
                textView.setSelected(true);
            } else {
                textView.setText(getString(R.string.tab_set));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_tab_setting_cake));
                icon.setSelected(false);
                textView.setSelected(false);
            }

            tab.setCustomView(inflate);
            binding.tab.addTab(tab);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        TPAdManager.INSTANCE.loadAllAd(this);
    }
}
