package com.example.wallpapermagazine.fragment;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

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;

public class PreviewFragment extends Fragment {
    private String source_url;
    private ImageView imageViewPreview;
    private ProgressBar progressBar;
    public PreviewFragment() {

    }
    public static PreviewFragment newInstance(String param1) {
        Bundle bundle = new Bundle();
        bundle.putString(StaticValue.key_source, param1);
        PreviewFragment fragment = new PreviewFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @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);
        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);
                    }
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                    }
                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        progressBar.setVisibility(View.GONE);
                    }
                });
        return fragmentview;
    }
}
