package com.app.wallpaper.myfra;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.app.wallpaper.R;
import com.app.wallpaper.databinding.FragmentHomeBinding;
import com.app.wallpaper.getdata.GetWalls;
import com.app.wallpaper.getdata.ReadFile;
import com.app.wallpaper.getdata.SetData;
import com.app.wallpaper.getdata.Urldata;
import com.app.wallpaper.myOnitem.OnItemHome;
import com.app.wallpaper.myact.ViewActivity;
import com.app.wallpaper.myada.TabPagerAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

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


public class HomeFragment extends Fragment implements OnItemHome {
    private FragmentHomeBinding binding;
    private DrawerLayout drawerLayout;
    private final List<Fragment> mfragmentList = new ArrayList<>();
    private List<GetWalls> datas = new ArrayList<>();
    private int position;
    private Random random;
    private List<Urldata> urldata = new ArrayList<>();
    private NavigationView navigationView;

    @SuppressLint("CheckResult")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(getLayoutInflater());
        binding.tablay.setupWithViewPager(binding.viewPagerTab);
        urldata = SetData.getDataUrls();
        drawerLayout = requireActivity().findViewById(R.id.drawer_layout);
        navigationView = requireActivity().findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.about) {
                    showDialog();
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    String packName = requireContext().getPackageName();
                    intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + packName));
                    startActivity(intent);
                }
                return false;
            }
        });


        random = new Random();
        position = random.nextInt(SetData.getDataUrls().size() - 1);
        defautIma();
        setFra();
        binding.viewPagerTab.setAdapter(new TabPagerAdapter(getChildFragmentManager(), mfragmentList, datas));
        binding.homeSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
        binding.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ViewActivity.class);
                intent.putExtra("position1", position);
                startActivity(intent);

            }
        });
        return binding.getRoot();
    }

    private void defautIma() {
        Glide.with(this)
                .load(urldata.get(position).getSourceUrl())
                .centerCrop()
                .placeholder(R.drawable.place)
                .into(binding.img);
    }

    private void setFra() {
        for (int i = 0; i < SetData.getCataDatas().size(); i++) {
            mfragmentList.add(new LoadDataFragment());
        }
        datas = SetData.getCataDatas();

    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.alterdialog, null);
        builder.setCustomTitle(dialogView);
        builder.setMessage("The current version is 1.0.0")
                .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void OnitemClick(int position) {
        Intent intent = new Intent(getContext(), ViewActivity.class);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}