package com.joyful.euphoria.board.xmlactivity;

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.MarginPageTransformer;

import com.joyful.euphoria.board.R;
import com.joyful.euphoria.board.databinding.ActivityHomePageXmlBinding;
import com.joyful.euphoria.board.utils.CustomUtils;
import com.joyful.euphoria.board.frag.FragmentCollect;
import com.joyful.euphoria.board.frag.HomePage;
import com.google.android.material.tabs.TabLayout;

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

public class HomePageXML extends AppCompatActivity {

    private ActivityHomePageXmlBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityHomePageXmlBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(vb.getRoot());
        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;
        });

        init();

    }

    private void init() {
        List<Fragment> listFragment = new ArrayList<>();
        listFragment.add(HomePage.newInstance());
        listFragment.add(FragmentCollect.newInstance());

        List<TabLayout.Tab> tabList = new ArrayList<>();

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

            View inflate = LayoutInflater.from(this).inflate(R.layout.tab_content, null, false);
            ImageView icon = inflate.findViewById(R.id.im_icon);
            CustomUtils textView = inflate.findViewById(R.id.textView);
            if (i == 0) {
                textView.setSelected(true);
                textView.setText(getString(R.string.tab_home));
                textView.setTextColor(ContextCompat.getColor(this, R.color.white));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_euphoria_tab_main));
            } else if(i ==1){
                textView.setSelected(false);
                textView.setText(getString(R.string.tab_love));
                textView.setTextColor(ContextCompat.getColor(this, R.color.tab_text));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_euphoria_tab_love));
            }
            tab.setCustomView(inflate);
            vb.tabLayout.addTab(tab);


        }


        vb.viewpager.setUserInputEnabled(false);
        vb.viewpager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

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

        vb.viewpager.setPageTransformer(new MarginPageTransformer(20));
        bing();
    }


    private void bing() {
        vb.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    ImageView icon = customView.findViewById(R.id.im_icon);
                    CustomUtils textView = customView.findViewById(R.id.textView);
                    icon.setSelected(true);
                    textView.setTextColor(ContextCompat.getColor(HomePageXML.this, R.color.white));
                }
                vb.viewpager.setCurrentItem(tab.getPosition(), true); // 添加平滑滚动
                // 更新标题
                updateTitle(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    ImageView icon = customView.findViewById(R.id.im_icon);
                    CustomUtils textView = customView.findViewById(R.id.textView);
                    icon.setSelected(false);
                    textView.setTextColor(ContextCompat.getColor(HomePageXML.this, R.color.tab_text));
                }
            }

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

        // 初始化第一个Tab为选中状态
        if (vb.tabLayout.getTabCount() > 0) {
            TabLayout.Tab firstTab = vb.tabLayout.getTabAt(0);
            if (firstTab != null) {
                firstTab.select();
            }
        }
    }
    private void updateTitle(int position) {
        if (position == 0) {
            vb.titleTv.setText(getString(R.string.app_name));
        }  else if(position == 1) {
            vb.titleTv.setText(getString(R.string.my_favorite));
        }
    }
}