package com.lh.wallpaper2.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.lh.wallpaper2.R;
import com.lh.wallpaper2.StaticValue;


public class PreviewFragment extends Fragment {


    private String source_url;

    private ImageView imageViewPreview;

    private ImageView preview;

    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(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            source_url = getArguments().getString(StaticValue.key_source);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View fragmentView = inflater.inflate(R.layout.fragment_preview, container, false);
        imageViewPreview = fragmentView.findViewById(R.id.imageview_preview);
        preview = fragmentView.findViewById(R.id.preview);

        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);
                        preview.setVisibility(View.GONE);

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });

        return fragmentView;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}