package com.viaccessorca.voplayerdemo.quickaction;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.viaccessorca.voplayerdemo.R;


public class ThumbnailHoverPopup {

    private final static String TAG = "ThumbnailHoverPopup";

    protected Context mContext;
    protected PopupWindow mWindow;
    private View mRootView;
    protected WindowManager mWindowManager;
    private LayoutInflater mInflater;

    private int rootWidth = 0;
    public View actionAnchor;

    private ImageView thumbnailPopupImg = null;
    private boolean displayed = false;


    public ThumbnailHoverPopup(Context context) {
        mWindow = new PopupWindow(context);
        this.mContext = context;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setRootViewId(R.layout.thumbnailpopup_view);
    }


    public void setRootViewId(int id) {
        mRootView = (ViewGroup) mInflater.inflate(id, null);
        thumbnailPopupImg = (ImageView) mRootView.findViewById(R.id.thumbnailPopupImgView);
        mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mWindow.setContentView(mRootView);
    }

    /**
     * Show PopupWindow in the correct place depending on parameters
     * @param anchor The view to take for reference
     * @param xPercent X position inside the anchor view as a percentage
     * @param yPercent Y position uppon the anchor view as a percentage
     */
    public void show(View anchor, int xPercent, int yPercent) {

        Log.d(TAG, "show " + xPercent);

        mWindow.setBackgroundDrawable(new BitmapDrawable());

        mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        int xPos, yPos;
        int[] location = new int[2];

        anchor.getLocationOnScreen(location);
        Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1]
                + anchor.getHeight());

        mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        int rootHeight = mRootView.getMeasuredHeight();
        if (rootWidth == 0) {
            rootWidth = mRootView.getMeasuredWidth();
        }

        int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
        int screenHeight = mWindowManager.getDefaultDisplay().getHeight();

        xPos = anchorRect.left + (int) (anchorRect.width() * (float) xPercent / 100.0) - rootWidth / 2;
        if (xPos + rootWidth > screenWidth) {
            xPos = screenWidth - rootWidth;
        }
        xPos = (xPos < 0) ? 0 : xPos;

        yPos = anchorRect.top - (int) (anchorRect.height() * (float) yPercent / 100.0) - rootHeight;

        if ((yPos + rootHeight) > screenHeight) {
            yPos = screenHeight - rootHeight;
        }
        yPos = (yPos < 0) ? 0 : yPos;

        if (!displayed) {
            mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
        } else {
            mWindow.update(xPos, yPos, mWindow.getWidth(), mWindow.getHeight());
        }
        displayed = true;
    }

    public void dismiss() {
        Log.d(TAG, "dismiss");
        displayed = false;
        mWindow.dismiss();
    }

    public void setThumbnailBitmap(Bitmap bitmap) {
        this.thumbnailPopupImg.setImageBitmap(bitmap);
    }
}
