package com.app.keyboard.colorful.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

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.app.keyboard.colorful.data.ClassBean;
import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.app.keyboard.colorful.MyApp;
import com.app.keyboard.colorful.R;
import com.app.keyboard.colorful.helper.CommonUtils;
import com.app.keyboard.colorful.databinding.ActivityMainBinding;
import com.app.keyboard.colorful.data.ChildBean;
import com.app.keyboard.colorful.afragment.FragmentLike;
import com.app.keyboard.colorful.afragment.FragmentMain;

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

public class ActivityMain extends AppCompatActivity {

    private ActivityMainBinding vb;


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

    }

    private void loadBanner() {
        List<ClassBean> list = new ArrayList<>();
        list.addAll(MyApp.list);
        Collections.shuffle(list);
        List<ChildBean> 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(ChildBean data, ImageView imageView) {
        String thumbGif = data.getThumbGif();
        String thumb = data.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            CommonUtils.INSTANCE.loadWepJif(this, thumbGif, imageView);
        } else {
            Glide.with(this).load(thumb)
                    .placeholder(R.drawable.def_placeholder)
                    .into(imageView);
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMain.this, ActivityDownload.class);
                intent.putExtra(ActivityDownload.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);
            LinearLayout layout = inflate.findViewById(R.id.linear);
//            TextView textView = inflate.findViewById(R.id.textView);
            if (i == 0) {
//                textView.setSelected(true);
//                textView.setText(getString(R.string.tv_home));
//                layout.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_home_tab));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.status_tab_home));
            } else if (i == 1) {
//                textView.setSelected(false);
//                textView.setText(getString(R.string.tv_like));
//                layout.setBackground(ContextCompat.getDrawable(this, R.drawable.selector_collection_tab));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.status_tab_collection));
            }
            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) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.im_icon);
                icon.setSelected(true);
                vb.viewpager.setCurrentItem(tab.getPosition());
                int position = tab.getPosition();
//                if(position == 0){
//                    vb.topTitle.setText(getString(R.string.tv_skins));
//                }else {
//                    vb.topTitle.setText(getString(R.string.tv_like));
//                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView icon = Objects.requireNonNull(tab.getCustomView()).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();
            }
        });
    }
}