package com.ar.drawing.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
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.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.ar.drawing.others.Utils;
import com.google.android.material.tabs.TabLayout;
import com.ar.drawing.R;
import com.ar.drawing.databinding.ActivityMainBinding;
import com.ar.drawing.fragment.FragmentFavorite;
import com.ar.drawing.fragment.FragmentHome;

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

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    private List<Fragment> listFragment = new ArrayList<>();


    private String[] permissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBarTextColor(this, true);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

//        fragmentAlbum = FragmentAlbum.newInstance();
        listFragment.add(FragmentHome.newInstance());
        listFragment.add(FragmentFavorite.newInstance());

        onTabInit();
        binding.viewPager2.setUserInputEnabled(false);
        binding.viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getItemCount() {
                return listFragment.size();
            }
        });
        bingTabVp();
        binding.btnImport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkPermission();
            }
        });

    }


    private void bingTabVp() {
        binding.tab.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);
                binding.viewPager2.setCurrentItem(tab.getPosition());
            }

            @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) {

            }
        });
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Objects.requireNonNull(binding.tab.getTabAt(position)).select();
            }
        });
    }


    private void onTabInit() {

        for (int i = 0; i < listFragment.size(); i++) {
            TabLayout.Tab tab = binding.tab.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.setText(getString(R.string.tv_tab1));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_tab));
            } else {
                textView.setText(getString(R.string.tv_like));
                icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.selector_like));
            }
            tab.setCustomView(inflate);
            binding.tab.addTab(tab);
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0) {
            if (Utils.checkPermission(MainActivity.this, permissions)) {
                startImport();
            } else {

            }

        }
    }

    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions = new String[]{Manifest.permission.READ_MEDIA_IMAGES};
        } else {
            permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        }
        if (!Utils.checkPermission(MainActivity.this, permissions)) {

            ActivityCompat.requestPermissions(MainActivity.this, permissions, 0);
        } else {

            startImport();
        }
    }

    private void startImport() {
        Intent intent = new Intent(MainActivity.this, ImportActivity.class);
        startActivity(intent);

    }
}