package com.ar.arsketch.activity;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.ar.arsketch.R;
import com.ar.arsketch.adapter.CategoryViewPager2Adapter;
import com.ar.arsketch.databinding.ActivityCategoryBinding;
import com.ar.arsketch.utils.Names;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.List;

public class CategoryActivity extends AppCompatActivity {

    private ActivityCategoryBinding binding;
    private final List<String> categories = Names.getAllDir();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCategoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initData();
        initEvent();

    }

    private void initData() {

        CategoryViewPager2Adapter adapter = new CategoryViewPager2Adapter(this, categories);
        binding.categoryViewPager.setAdapter(adapter);

    }

    private void initEvent() {

        binding.categoryBack.setOnClickListener(v -> finish());

        binding.categoryBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        new TabLayoutMediator(binding.categoryTab, binding.categoryViewPager, (tab, position) -> {

            View customView = LayoutInflater.from(this).inflate(R.layout.category_tab_custom, null);
            tab.setCustomView(customView);

            TextView textView = customView.findViewById(R.id.category_tab_custom_title);
            String dir = categories.get(position);
            String text = dir.substring(dir.lastIndexOf("_") + 1);
            text = text.substring(0, 1).toUpperCase() + text.substring(1);
            textView.setText(text);

        }).attach();

        binding.categoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView textView = customView.findViewById(R.id.category_tab_custom_title);
                    textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.category_color));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView textView = customView.findViewById(R.id.category_tab_custom_title);
                    textView.setTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.black));
                }
            }

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

        TabLayout.Tab firstTab = binding.categoryTab.getTabAt(0);
        if (firstTab != null) {
            firstTab.select();
            View customView = firstTab.getCustomView();
            if (customView != null) {
                TextView textView = customView.findViewById(R.id.category_tab_custom_title);
                textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.category_color));
            }

        }

    }

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

}