package com.app.wave.keyboard.ui;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
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 com.ad.tradpluslibrary.TPAdManager;
import com.app.wave.keyboard.R;
import com.app.wave.keyboard.databinding.ActivityMainBinding;

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

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding vb;
    private ImageView homeIcon, favoriteIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMainBinding.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();
    }

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

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

        // 获取容器视图
        View tabHomeContainer = findViewById(R.id.tab_home_container);
        View tabFavoriteContainer = findViewById(R.id.tab_favorite_container);

        // 获取图标
        homeIcon = findViewById(R.id.tab_home).findViewById(R.id.im_icon);
        favoriteIcon = findViewById(R.id.tab_favorite).findViewById(R.id.im_icon);

        // 设置图标
        homeIcon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.item_hometab));
        favoriteIcon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.item_favouritetab));

        // 初始状态：第一个选中
        homeIcon.setSelected(true);
        favoriteIcon.setSelected(false);

        int desiredIconSize = dpToPx(36);

        ViewGroup.LayoutParams homeParams = homeIcon.getLayoutParams();
        homeParams.width = desiredIconSize;
        homeParams.height = desiredIconSize;
        homeIcon.setLayoutParams(homeParams);

        ViewGroup.LayoutParams favoriteParams = favoriteIcon.getLayoutParams();
        favoriteParams.width = desiredIconSize;
        favoriteParams.height = desiredIconSize;
        favoriteIcon.setLayoutParams(favoriteParams);

        // 设置点击监听（监听容器而不是内部视图）
        tabHomeContainer.setOnClickListener(v -> {
            vb.viewpager.setCurrentItem(0);
            updateTabSelection(true);
        });

        tabFavoriteContainer.setOnClickListener(v -> {
            vb.viewpager.setCurrentItem(1);
            updateTabSelection(false);
        });

        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.setCurrentItem(0);
        updateTabSelection(true);
    }

    private void updateTabSelection(boolean isHomeSelected) {
        if (homeIcon != null && favoriteIcon != null) {
            homeIcon.setSelected(isHomeSelected);
            favoriteIcon.setSelected(!isHomeSelected);
        }
    }

    // DP转PX的辅助方法
    private int dpToPx(int dp) {
        float density = getResources().getDisplayMetrics().density;
        return Math.round(dp * density);
    }
}