package com.key.vibekeyboard.ui.dialog;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.key.vibekeyboard.ui.activity.DownloadActivity;
import com.key.vibekeyboard.R;
import com.key.vibekeyboard.data.database.entity.WallpaperInfo;
import com.key.vibekeyboard.databinding.DialogRecommendedBinding;

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

public class RecommendedDialog extends DialogFragment {

    private DialogRecommendedBinding binding;
    private List<WallpaperInfo> list = new ArrayList<>();
    private WallpaperInfo wallpaperInfo;

    public void setList(List<WallpaperInfo> list) {
        this.list = list;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogRecommendedBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Dialog dialog = getDialog();
        setCancelable(true);
        if (dialog != null && dialog.getWindow() != null) {
            Window window = dialog.getWindow();
            window.setLayout((int) (getResources().getDisplayMetrics().widthPixels * 0.9), WindowManager.LayoutParams.WRAP_CONTENT);
        }

        Random random = new Random();
        int randomIndex = random.nextInt(list.size());
        wallpaperInfo = list.get(randomIndex);

        List<WallpaperInfo> limitedList = list.subList(0, Math.min(20, list.size()));

        loadImage();

        binding.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), DownloadActivity.class);
                intent.putExtra("wallpaperInfo", wallpaperInfo);
                intent.putParcelableArrayListExtra("list", new ArrayList<>(limitedList));
                dismiss();
                requireContext().startActivity(intent);
            }
        });
    }

    private void loadImage() {
        if (wallpaperInfo.getPreview() != null && !wallpaperInfo.getPreview().isEmpty()) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.splash)
                    .error(R.mipmap.splash)
                    .transform(new CenterCrop())
                    .transform(new RoundedCorners(16));

            Glide.with(requireContext())
                    .asDrawable()
                    .load(wallpaperInfo.getPreview())
                    .apply(options)
                    .into(binding.imageView);
        } else {
            binding.imageView.setImageResource(R.mipmap.splash);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
