package de.tudarmstadt.informatik.hostage.ui2.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/**
 * Created by Julien on 13.02.14.
 */
public abstract class AbstractPopup {

    static final int ORIENTATION_LANDSCAPE = 2;

    public interface OnPopupItemClickListener {
        public void onItemClick(Object data);
    }

    private PopupWindow popupWindow;
    private Activity context;
    private OnPopupItemClickListener onPopupItemClickListener;
    private LinearLayout rootView;
    private LayoutInflater lInf;

    abstract public int getLayoutId();
    abstract void configureView(View view);

    public AbstractPopup(Context context, OnPopupItemClickListener listener) {
        super();
        this.context = (Activity) context;
        this.onPopupItemClickListener = listener;
        this.popupWindow = new PopupWindow(context);
        this.popupWindow.setOutsideTouchable(true);
        this.popupWindow.setFocusable(true);
        this.lInf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public void addItem(final AbstractPopupItem item)	{
        View view = item.getItemView();

        if (this.rootView == null){
            this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
            this.configureView(this.rootView);
        }
        if (this.rootView != null){
            this.rootView.addView(view);
            view.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
	                if(event.getAction() == MotionEvent.ACTION_DOWN){
						item.onItemDown(event);
	                } else if (event.getAction() == MotionEvent.ACTION_UP){
	                    item.onItemUp(event);

                        AbstractPopup.this.onPopupItemClickListener.onItemClick(item.onClickedResult(event));
                        AbstractPopup.this.popupWindow.dismiss();
                    }
                    return true;
                }
            });
        }

    }

    public View getPopupView(){
        if (this.rootView == null){
            this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
        }
        return this.rootView;
    }

    public void showOnView(final View anchorView)	{
        if (this.rootView == null){
            this.rootView = (LinearLayout) this.lInf.inflate(this.getLayoutId(), null);
        }
        if (this.rootView != null){
            AbstractPopup.this.popupWindow.dismiss();

            this.popupWindow.setContentView(this.rootView);

            final Rect windowFrame= new Rect();

            Window window = this.context.getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(windowFrame);
            int orientation = 1; //this.context.getResources().getConfiguration().orientation;
            int windowWidth = orientation == ORIENTATION_LANDSCAPE ? windowFrame.height() : windowFrame.width();
            int windowHeight = orientation == ORIENTATION_LANDSCAPE ? windowFrame.width() : windowFrame.height();

            final int[] position = new int[2];
            anchorView.getLocationOnScreen(position);
            final int anchorWidth = anchorView.getWidth();

            this.rootView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
            int width = this.rootView.getMeasuredWidth();
            int height = this.rootView.getMeasuredHeight();

            //int alh = (position[0] + width) - windowFrame.width();
            //if (alh < 0) alh = 0;

            int x = position[0] + (anchorWidth / 2) - (width / 2);
            int y = position[1] - height;

            x = Math.max(0, x);
            x = Math.min(windowWidth - width, x);

            y = Math.min(0,y);
            y = Math.max(windowHeight - height, y);

            AbstractPopup.this.configureView(this.rootView);

            int smallBottomOffset = 45;
            this.popupWindow.setWidth(width);
            this.popupWindow.setHeight(height);
            this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);



            /*
            // TO SET THE REAL SIZE
            ViewTreeObserver viewTreeObserver = this.rootView.getViewTreeObserver();
            if (viewTreeObserver != null &&  viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        ViewTreeObserver observer= AbstractPopup.this.rootView.getViewTreeObserver();
                        if (observer != null)observer.removeOnGlobalLayoutListener(this);

                        View rootView = AbstractPopup.this.rootView;

                        int width = rootView.getWidth();
                        int height = rootView.getHeight();

                        int x = position[0] + (anchorWidth / 2) - (width / 2);
                        int y = position[1] - height;

                        x = Math.min(0,x);
                        x = Math.max(windowFrame.width() - width, x);

                        y = Math.min(0,y);
                        y = Math.max(windowFrame.height() - height, y);

                        AbstractPopup.this.configureView(rootView);

                        int smallBottomOffset = 10;
                        AbstractPopup.this.popupWindow.dismiss();
                        AbstractPopup.this.popupWindow.setWidth(width);
                        AbstractPopup.this.popupWindow.setHeight(height + smallBottomOffset);
                        AbstractPopup.this.popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y-smallBottomOffset);

                    }
                });
            }
            */
        }
    }


}
