package com.example.wallpapermagazine.fragment;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.wallpapermagazine.R;
import com.example.wallpapermagazine.StaticValue;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class PreviewFragment2 extends Fragment {
    private String source_url;
    private ImageView imageViewPreview;
    private ProgressBar progressBar;
    private FloatingActionButton floatingActionButton;
    public PreviewFragment2() {

    }
    public static PreviewFragment2 newInstance(String param1) {
        Bundle bundle = new Bundle();
        bundle.putString(StaticValue.key_source, param1);
        PreviewFragment2 fragment2 = new PreviewFragment2();
        fragment2.setArguments(bundle);
        return fragment2;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            source_url = getArguments().getString(StaticValue.key_source);
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View fragmentview = inflater.inflate(R.layout.fragment_img, container, false);
        imageViewPreview = fragmentview.findViewById(R.id.imageview_preview);
        progressBar = fragmentview.findViewById(R.id.progressbar);
        floatingActionButton = getActivity().findViewById(R.id.fbt_love);
        Glide.with(requireContext())
                .asBitmap()
                .load(source_url)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        imageViewPreview.setImageBitmap(resource);
                        progressBar.setVisibility(View.GONE);
                        requireActivity().findViewById(R.id.set_wallpaper).setEnabled(true);
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                        requireActivity().findViewById(R.id.set_wallpaper).setEnabled(false);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        progressBar.setVisibility(View.GONE);
                        requireActivity().findViewById(R.id.set_wallpaper).setEnabled(true);
                    }
                });
        return fragmentview;
    }
}
