package com.sobot.album.app.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.sobot.album.AlbumFile;
import com.sobot.album.R;
import com.sobot.album.SobotAlbum;
import com.sobot.album.widget.photoview.AttacherImageView;
import com.sobot.album.widget.photoview.PhotoViewAttacher;

public class ImageFragment extends Fragment implements PhotoViewAttacher.OnViewTapListener, View.OnLongClickListener {
    private View mRootView;
    private AttacherImageView imageView;
    public AlbumFile mAlbum;
    private View.OnClickListener mItemClickListener;
    private View.OnClickListener mItemLongClickListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_img_item, container, false);
        imageView = mRootView.findViewById(R.id.sobot_item_iv);
        final PhotoViewAttacher attacher = new PhotoViewAttacher(imageView);
        attacher.setOnViewTapListener(this);
        attacher.setOnLongClickListener(this);
        imageView.setAttacher(attacher);
        initDate();
        return mRootView;
    }

    public static ImageFragment getInstance(AlbumFile mAlbum) {
        ImageFragment fragment = new ImageFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("mAlbum", mAlbum);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setArguments(@Nullable Bundle bundle) {
        super.setArguments(bundle);
        if (bundle != null) {
            mAlbum = bundle.getParcelable("mAlbum");
        }
    }

    private void initDate() {
        if (mAlbum != null) {
            SobotAlbum.getAlbumConfig().getAlbumLoader().load(imageView, mAlbum);
        }
    }

    @Override
    public void onViewTap(View v, float x, float y) {
        if (mItemClickListener != null) {
            mItemClickListener.onClick(v);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mItemLongClickListener != null) {
            mItemLongClickListener.onClick(v);
        }
        return true;
    }

    /**
     * Set item click listener.
     *
     * @param onClickListener listener.
     */
    public void setItemClickListener(View.OnClickListener onClickListener) {
        this.mItemClickListener = onClickListener;
    }

    /**
     * Set item long click listener.
     *
     * @param longClickListener listener.
     */
    public void setItemLongClickListener(View.OnClickListener longClickListener) {
        this.mItemLongClickListener = longClickListener;
    }

}
