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 androidx.recyclerview.widget.StaggeredGridLayoutManager;

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.FragmentMyfav2Binding;
import com.app.wallpaper.getdata.SetData;
import com.app.wallpaper.myOnitem.OnFavority;
import com.app.wallpaper.myOnitem.OnItemHome;
import com.app.wallpaper.myOnitem.OnLoaditem;
import com.app.wallpaper.myact.ViewActivity;
import com.app.wallpaper.myada.LoadDataAdapter;
import com.app.wallpaper.myada.MyfavorityAdapter;
import com.app.wallpaper.mydao.AppDatabase;
import com.app.wallpaper.mydao.MyEntity;
import com.app.wallpaper.utils.MyItemDecoration;
import com.google.android.material.navigation.NavigationView;

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

public class MyfavFragment extends Fragment implements OnFavority {

    private FragmentMyfav2Binding binding;
    private AppDatabase appDatabase;
    private List<MyEntity> datas = new ArrayList<>();
    private MyfavorityAdapter myfavorityAdapter;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MyfavFragment() {
        // Required empty public constructor
    }

    public static MyfavFragment newInstance(String param1, String param2) {
        MyfavFragment fragment = new MyfavFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMyfav2Binding.inflate(getLayoutInflater());
        appDatabase = AppDatabase.getDatabase(requireContext());
        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;
            }
        });
        binding.myfraSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout= getActivity().findViewById(R.id.drawer_layout);
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
        setRecyclerview();
        loadFavoriteImages();
        return binding.getRoot();
    }
    private void loadFavoriteImages() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<MyEntity> allFavoriteImages = appDatabase.interfaceDao().getAllImages();
                requireActivity().runOnUiThread(new Runnable() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void run() {
                        if (allFavoriteImages.isEmpty()){
                            binding.layout.setVisibility(View.VISIBLE);;
                        }else {
                            binding.layout.setVisibility(View.GONE);
                        }
                        datas.clear();
                        datas.addAll(allFavoriteImages);
                        myfavorityAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }

    private void setRecyclerview() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        binding.myfra.setLayoutManager(staggeredGridLayoutManager);
        myfavorityAdapter = new MyfavorityAdapter(getContext(), datas);
        myfavorityAdapter.setInfoWallPaperListener((OnFavority) this);
        binding.myfra.addItemDecoration(new MyItemDecoration(SetData.getContext(),20,20,15));
        binding.myfra.setAdapter(myfavorityAdapter);
    }
    @Override
    public void OnitemClick(MyEntity myEntity,int Id) {
        Intent intent = new Intent(getContext(), ViewActivity.class);
        intent.putExtra("imageUrl",myEntity);
        intent.putExtra("Id", Id);
        startActivity(intent);
    }
    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 onResume() {
        super.onResume();
        loadFavoriteImages();
    }
}