package com.free.life.wallpaper.ui;

import android.os.Bundle;
import android.view.View;

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 com.free.life.wallpaper.databinding.ActivityMainBinding;
import com.free.life.wallpaper.databinding.TabCustomViewBinding;
import com.google.android.material.tabs.TabLayout;
import com.free.life.wallpaper.R;
import com.free.life.wallpaper.utils.CommonUtils;

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

public class MainActivity extends AppCompatActivity  {


    private ActivityMainBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityMainBinding.inflate(getLayoutInflater());
        CommonUtils.initFull(this,false);
        setContentView(vb.getRoot());


        List<Fragment> list = new ArrayList<>();
        list.add(FragmentHome.newInstance(0));
        list.add(FragmentLike.newInstance());
//        list.add(FragmentDownload.newInstance());

        int[] ints = {R.drawable.selector_home, R.drawable.selector_like};
        vb.viewpager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return list.get(position);
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        });
        vb.viewpager2.setUserInputEnabled(false);

        for(int i = 0;i<list.size();i++){
            TabLayout.Tab tab = vb.tab.newTab();
            TabCustomViewBinding tabVb = TabCustomViewBinding.inflate(getLayoutInflater());
            tabVb.imageIcon.setImageDrawable(ContextCompat.getDrawable(this,ints[i]));
            tab.setCustomView(tabVb.getRoot());
            if(i ==0){
                tabVb.imageIndicator.setVisibility(View.VISIBLE);
            }else {
                tabVb.imageIndicator.setVisibility(View.GONE);
            }
            vb.tab.addTab(tab);
        }
        vb.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                vb.viewpager2.setCurrentItem(position);

                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                View imageIndicator = customView.findViewById(R.id.image_indicator);
                imageIndicator.setVisibility(View.INVISIBLE);
            }

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

            }
        });


    }


    @Override
    protected void onResume() {
        super.onResume();
        /**
         *  Total PSS: 144060 KB
         * 2024-12-08 21:39:03.211 30266-30266 MemoryInfo              com.hd.phone.mico.test               D  Total Private Dirty: 121456 KB
         * 2024-12-08 21:39:03.211 30266-30266 MemoryInfo              com.hd.phone.mico.test               D  Total Shared Dirty: 22320 KB
         */
        CommonUtils.printUsedMemory(this);
    }
}