package com.keypalette.theme.activity;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.keypalette.theme.FragmentLike;
import com.keypalette.theme.FragmentMain;
import com.keypalette.theme.KeyApp;
import com.keypalette.theme.R;
import com.keypalette.theme.databinding.PageMainBinding;
import com.keypalette.theme.dataabout.resource.Data;
import com.keypalette.theme.dataabout.resource.WrapperData;
import com.keypalette.theme.other.Helper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class PageMain extends AppCompatActivity {

    private PageMainBinding vb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = PageMainBinding.inflate(getLayoutInflater());
        Helper.INSTANCE.initFullScreen(this, true);
        setContentView(vb.getRoot());
        loadBanner();
        init();

    }

    private void loadBanner() {
        List<WrapperData> list = new ArrayList<>();
        list.addAll(KeyApp.list);
        Collections.shuffle(list);
        List<Data> keyboardList = list.get(0).getKeyboardList();
        Collections.shuffle(keyboardList);

//        loadThumb(keyboardList.get(0), vb.im1);
//        loadThumb(keyboardList.get(1), vb.im2);
//        loadThumb(keyboardList.get(2), vb.im3);
//        loadThumb(keyboardList.get(3), vb.im4);
//        loadThumb(keyboardList.get(4), vb.im5);
//
//        vb.viewFlipper.startFlipping();
    }

//    private void loadThumb(Child data, ImageView imageView) {
//        String thumbGif = data.getThumbGif();
//        String thumb = data.getThumbUrl();
//        if (!thumbGif.isEmpty()) {
//            Common.INSTANCE.loadWepJif(this, thumbGif, imageView);
//        } else {
//            Glide.with(this).load(thumb)
//                    .placeholder(R.drawable.image_def_placeholder)
//                    .into(imageView);
//        }
//        imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(ActMain.this, ActDownload.class);
//                intent.putExtra(ActDownload.SOURCE_KEY, data);
//                startActivity(intent);
//            }
//        });
//    }


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

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

            View inflate = LayoutInflater.from(this).inflate(R.layout.item_tab_view, null, false);
            ImageView icon = inflate.findViewById(R.id.im_icon);
            TextView textView = inflate.findViewById(R.id.textView);
            if (i == 0) {
                textView.setSelected(true);
                textView.setText(getString(R.string.tab_home));
//                layout.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_home_tab));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_main));
            } else if (i == 1) {
                textView.setSelected(false);
                textView.setText(getString(R.string.tab_collection));
//                layout.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_collection_tab));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_like));
            }
            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();
            }
        });
        bing();
    }


    private void bing() {
        vb.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = Objects.requireNonNull(tab.getCustomView());
                ImageView icon = view.findViewById(R.id.im_icon);
                icon.setSelected(true);
                vb.viewpager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = Objects.requireNonNull(tab.getCustomView());
                ImageView icon = view.findViewById(R.id.im_icon);
                icon.setSelected(false);

            }

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

            }
        });
        vb.viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Objects.requireNonNull(vb.tabLayout.getTabAt(position)).select();
            }
        });
    }
}