package com.tool.applockpro.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.tool.applockpro.R;
import com.tool.applockpro.adapter.PageAdapter;
import com.tool.applockpro.databinding.ActivityMainBinding;
import com.tool.applockpro.fragment.AllFragment;
import com.tool.applockpro.tool.Myservice;

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

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    private String[] listtext = {"System", "Third", "Lock"};
    private List<AllFragment> list = new ArrayList<>();
    private PageAdapter adapter;
    private TypedArray imageArray;
    private ImageView tabimg;
    private TextView tabtext;
    private ConstraintLayout imagebg;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        startService(new Intent(this, Myservice.class));
        initTab();

        binding.mainSeeting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });


        binding.mainSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initTab() {

        for (String s : listtext) {
            list.add(AllFragment.newInstance(s));
        }
        adapter = new PageAdapter(getSupportFragmentManager(), list);
        imageArray = getResources().obtainTypedArray(R.array.drawable_images);
        binding.mainViewpager.setAdapter(adapter);
        for (int i = 0; i < 3; i++) {
            TabLayout.Tab tab = binding.mainTabLayout.newTab();
            binding.mainTabLayout.addTab(tab);
            View customView = LayoutInflater.from(this).inflate(R.layout.item_tablayout, null);
            tabimg = customView.findViewById(R.id.tab_image);
            tabtext = customView.findViewById(R.id.tab_text);
            imagebg = customView.findViewById(R.id.tab_cons);
            tabtext.setText(listtext[i]);
            Drawable drawable = imageArray.getDrawable(i);
            tabimg.setBackground(drawable);
            tab.setCustomView(customView);
            final int finalI = i;
            tab.view.setOnClickListener(v -> {
                binding.mainViewpager.setCurrentItem(finalI);
            });
        }
//        binding.mainViewpager.setOffscreenPageLimit(1);
        binding.mainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTabIcons(position);
                if (position == 1) {
                    AllFragment fragment =  list.get(1);
                    fragment.refesh();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setTabIcons(int position) {

        for (int i = 0; i < binding.mainTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = binding.mainTabLayout.getTabAt(i);
            if (tab != null) {
                View customView = tab.getCustomView();
                if (i == position) {
                    customView.findViewById(R.id.custom_tablayout).setSelected(true);
                    customView.findViewById(R.id.tab_cons).setSelected(true);
                    customView.findViewById(R.id.tab_text).setSelected(true);
                } else {
                    customView.findViewById(R.id.custom_tablayout).setSelected(false);
                    customView.findViewById(R.id.tab_cons).setSelected(false);
                    customView.findViewById(R.id.tab_text).setSelected(false);
                }
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (imageArray != null) {
            imageArray.recycle();
        }
    }

}