package com.hikapro.backpack.presenter.adapters.helper.items.swipe2;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by tariel on 18/06/16.
 */
public class SwipeMenuLayout extends LinearLayout {

    private SwipeMenu swipeMenu;
    private SwipableElement swipableElement;

    public SwipeMenuLayout(Context context) {
        super(context);
    }

    public SwipeMenuLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SwipeMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setSwipableElement(SwipableElement swipableElement) {
        this.swipableElement = swipableElement;
    }

    // adding menu with items
    public void addMenu(SwipeMenu menu) {
        swipeMenu = menu;
        List<SwipeMenuItem> items = menu.getMenuItems();
        int id = 0;
        for (SwipeMenuItem item : items) {
            addItem(item, item.getId() == 0 ? ++id : item.getId());
        }
    }

    private void addItem(SwipeMenuItem item, int id) {

        LayoutParams params = new LayoutParams(item.getWidth(),LayoutParams.MATCH_PARENT);
        LinearLayout parent = new LinearLayout(getContext());
        parent.setId(id);
        parent.setGravity(Gravity.CENTER);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setLayoutParams(params);
        parent.setBackgroundDrawable(item.getBackground());
        addView(parent);

        if (item.getIcon() != null) {
            parent.addView(createIcon(item));
        }
        if (!TextUtils.isEmpty(item.getTitle())) {
            parent.addView(createTitle(item));
        }
    }
    // adding menu with items + listener
    public void addMenu(SwipeMenu menu, OnClickListener listener) {
        swipeMenu = menu;
        List<SwipeMenuItem> items = menu.getMenuItems();
        int id = 0;
        for (SwipeMenuItem item : items) {
            addItem(item, item.getId() == 0 ? ++id : item.getId(), listener);
        }
    }

    private void addItem(SwipeMenuItem item, int id, OnClickListener listener) {

        LayoutParams params = new LayoutParams(item.getWidth(),LayoutParams.MATCH_PARENT);
        LinearLayout parent = new LinearLayout(getContext());
        parent.setId(id);
        parent.setGravity(Gravity.CENTER);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setLayoutParams(params);
        parent.setBackgroundDrawable(item.getBackground());
        parent.setOnClickListener(listener);
        addView(parent);

        if (item.getIcon() != null) {
            parent.addView(createIcon(item));
        }
        if (!TextUtils.isEmpty(item.getTitle())) {
            parent.addView(createTitle(item));
        }
    }

    public void notifyOnMenuItemClick() {
        if (swipableElement != null && swipableElement.isOpen())
            swipableElement.smoothCloseMenu();
    }

    private ImageView createIcon(SwipeMenuItem item) {
        ImageView iv = new ImageView(getContext());
        iv.setImageDrawable(item.getIcon());
        return iv;
    }

    private TextView createTitle(SwipeMenuItem item) {
        TextView tv = new TextView(getContext());
        tv.setText(item.getTitle());
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(item.getTitleSize());
        tv.setTextColor(item.getTitleColor());
        return tv;
    }

    private View findViewAtPoint(float x, float y) {
        View ret = null;
        View buf;
        for (int i = 0; i < this.getChildCount(); ++i) {
            buf = getChildAt(i);
            if (isPointInsideView(x, y, buf)) {
                ret = buf;
                break;
            }
        }
        return ret;
    }

    private boolean isPointInsideView(float x, float y, View view) {
        int location[] = new int[2];
        view.getLocationOnScreen(location);
        int viewX = location[0];
        int viewY = location[1];

        // point is inside view bounds
        return ((x > viewX && x < (viewX + view.getWidth())) &&
                (y > viewY && y < (viewY + view.getHeight())));
    }

}
