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

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import de.tudarmstadt.informatik.hostage.R;

/**
 * Created by Julien on 16.02.14.
 */
public class SplitPopupItem extends AbstractPopupItem {

    public final static String LEFT_TITLE = "LEFT_TITLE";
    public final static String RIGHT_TITLE = "RIGHT_TITLE";
    public final static String LEFT_SUBTITLE = "LEFT_SUBTITLE";
    public final static String RIGHT_SUBTITLE = "RIGHT_SUBTITLE";

    public boolean wasRightTouch;

	private Context context;
	private View left_container;
	private View right_container;

    public SplitPopupItem(Context context){
        super(context);

	    this.context = context;
    }

    public int getLayoutId(){
        return R.layout.split_popup_item;
    }

    public void configureItemView(View view){
        String leftTitle = (String) this.data.get(LEFT_TITLE);
        String rightTitle = (String) this.data.get(RIGHT_TITLE);

        String leftSubtitle = (String) this.data.get(LEFT_SUBTITLE);
        String rightSubtitle = (String) this.data.get(RIGHT_SUBTITLE);

        TextView leftTitleView = (TextView)view.findViewById(R.id.left_title_text_view);
        leftTitleView.setText(leftTitle);
        TextView leftSubtitleView = (TextView)view.findViewById(R.id.left_subtitle_text_view);
        if (leftSubtitle != null){
            leftSubtitleView.setText(leftSubtitle);
        } else {
            leftSubtitleView.setText("-");
        }

        TextView rightTitleView = (TextView)view.findViewById(R.id.right_title_text_view);
        rightTitleView.setText(rightTitle);
        TextView rightSubtilteView = (TextView)view.findViewById(R.id.right_subtitle_text_view);
        if (rightSubtitle != null){
            rightSubtilteView.setText(rightSubtitle);
        } else {
            rightSubtilteView.setText("-");
        }
    }

    public Object onClickedResult(MotionEvent event){
        this.wasRightTouch = isRightTouch(event);
        return this;
    }

	private boolean isRightTouch(MotionEvent event){
		return event.getX() > this.getItemView().getX() + (this.getItemView().getWidth() / 2);
	}

	private View getLeftContainer(){
		if(left_container == null){
			left_container = this.getItemView().findViewById(R.id.popup_left_container);
		}

		return left_container;
	}


	private View getRightContainer(){
		if(right_container == null){
			right_container = this.getItemView().findViewById(R.id.popup_right_container);
		}

		return right_container;
	}

	public void onItemDown(MotionEvent event){
		int blue_color = context.getResources().getColor(android.R.color.holo_blue_light);
		int trans_color = context.getResources().getColor(android.R.color.transparent);

		if(!isRightTouch(event)){
			getLeftContainer().setBackgroundColor(blue_color);
			getRightContainer().setBackgroundColor(trans_color);
		} else {
			getLeftContainer().setBackgroundColor(trans_color);
			getRightContainer().setBackgroundColor(blue_color);
		}
	}

	public void onItemUp(MotionEvent event){
		int trans_color = context.getResources().getColor(android.R.color.transparent);

		getLeftContainer().setBackgroundColor(trans_color);
		getRightContainer().setBackgroundColor(trans_color);
	}
}
