package com.key.simplekeyboard.activity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

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

import com.google.android.material.tabs.TabLayout;
import com.key.simplekeyboard.R;
import com.key.simplekeyboard.adapter.PageAdapter;
import com.key.simplekeyboard.fragment.LikeFragment;
import com.key.simplekeyboard.fragment.MainFragment;
import com.key.simplekeyboard.fragment.SettingFragment;

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


public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main_activity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
        initData();



    }

    private void initData() {
        PageAdapter adapter = new PageAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        setmainTabIcons(tabLayout);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setTabIcons(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initView() {
        tabLayout = findViewById(R.id.main_tabLayout);
        viewPager = findViewById(R.id.main_viewpager);
        list.add(new MainFragment());
        list.add(new LikeFragment());
        list.add(new SettingFragment());
    }
    private void setTabIcons(final int position) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            final TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                switch (i) {
                    case 0:
                        tab.setIcon(position == 0 ? R.drawable.home_check : R.drawable.home_uncheck);
                        break;
                    case 1:
                        tab.setIcon(position == 1 ? R.drawable.like_check : R.drawable.like_uncheck);
                        break;
                    case 2:
                        tab.setIcon(position == 2 ? R.drawable.setting_check : R.drawable.setting_uncheck);
                        break;
                    default:
                        break;
                }
            }
        }
    }
    private void setmainTabIcons(TabLayout tabLayout) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                switch (i) {
                    case 0:
                        tab.setIcon(R.drawable.home_check);
                        break;
                    case 1:
                        tab.setIcon(R.drawable.like_uncheck);
                        break;
                    case 2:
                        tab.setIcon(R.drawable.setting_uncheck);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}