package com.wallpaper.perfectwallpaper.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.wallpaper.perfectwallpaper.R;
import com.wallpaper.perfectwallpaper.activity.SettingActivity;
import com.wallpaper.perfectwallpaper.adapter.HomeFragmentAdapter;
import com.wallpaper.perfectwallpaper.databinding.FragmentHomeBinding;
import com.wallpaper.perfectwallpaper.databinding.ItemHomeCategoryBinding;


public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private final int[] types = {1, 2, 3};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);

        initData();
        initEvent();

        return binding.getRoot();
    }

    private void initData() {

        HomeFragmentAdapter adapter = new HomeFragmentAdapter(requireActivity(), types);
        binding.viewpager.setAdapter(adapter);

    }

    private void initEvent() {

        binding.setting.setOnClickListener(v -> {

            Intent intent = new Intent(requireActivity(), SettingActivity.class);
            requireActivity().startActivity(intent);

        });

        new TabLayoutMediator(binding.tablayout, binding.viewpager, (tab, position) -> {
            ItemHomeCategoryBinding tabBinding = ItemHomeCategoryBinding.inflate(LayoutInflater.from(requireContext()));
            tab.setCustomView(tabBinding.getRoot());
            setTabIconAndDotVisibility(tabBinding, position);
        }).attach();

        binding.tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabIcon(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabIcon(tab, false);
            }

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

            private void updateTabIcon(TabLayout.Tab tab, boolean isSelected) {
                if (tab.getCustomView() != null) {
                    ItemHomeCategoryBinding tabBinding = ItemHomeCategoryBinding.bind(tab.getCustomView());
                    int textColor = getTextColor(isSelected);
                    tabBinding.text.setTextColor(getResources().getColor(textColor, null));
                }
            }

            private int getTextColor(boolean isSelected) {
                return isSelected ? R.color.custom1 : R.color.custom2;
            }
        });
    }

    private void setTabIconAndDotVisibility(ItemHomeCategoryBinding tabBinding, int position) {
        switch (position) {
            case 0:
                tabBinding.text.setText(R.string.New);
                tabBinding.text.setTextColor(getResources().getColor(R.color.custom1));
                break;
            case 1:
                tabBinding.text.setText(R.string.Popular);
                break;
            case 2:
                tabBinding.text.setText(R.string.Random);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}