package com.wasu.cs.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.KeyEvent;

import com.wasu.cs.adapter.GridExAdapterBase;
import com.wasu.widget.FocusGridView;

import cn.com.wasu.main.BuildConfig;
import cn.com.wasu.main.ChannelFlavor;

/**
 * Created by mateng on 7/8/15.
 */
public class FocusGridViewEx extends FocusGridView {
    private FocusGridViewColumnListener mGridViewColumnListener;
    /**
	 * gridView优化方法 by danxingxi
	 * 由于gridview在滚动的时候会不断计算要显示的item的数量，需要不断调用onMeasure方法来计算
	 * 在调用onMeasure的时候会去调用gridview适配器的getView方法来获得子控件的width和height值，
	 * 会导致gridview在调用onMeasure的时候做了没必要的getView，我们一般都在getView中去加载了数据，
	 * 导致做了很多没必要的数据请求和操作，item数量过多的时候滚动会卡顿，我们在OnMeasure的时候拦截getView
	 * 中的数据请求和操作，在onLayout的时候再去数据请求和操作。
	 */
	/**isOnMeasure标志gridview是在OnMeasure中**/
//	public boolean isOnMeasure = false;
    public FocusGridViewEx(Context context) {
        super(context);
    }

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

    public GridExAdapterBase getGridAdapter() {
        return (GridExAdapterBase)getAdapter();
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	// TODO Auto-generated method stub
//    	isOnMeasure = true;
    	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    	// TODO Auto-generated method stub
//    	isOnMeasure = false;
    	super.onLayout(changed, l, t, r, b);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //针对乐视的渠道进行焦点的特殊处理
        if (BuildConfig.FLAVOR.equalsIgnoreCase(ChannelFlavor.LETV_MARKET) && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (isSelectedFirstItem() && keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                if (null != mGridViewColumnListener) {
                    mGridViewColumnListener.onColumnBegin();
                    return true;
                }
            } else if (isSelectedlastItem() && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
                if (null != mGridViewColumnListener) {
                    mGridViewColumnListener.onColumnEnd();
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean isSelectedFirstItem() {

        if (getNumColumns() == 1 || ((getSelectedItemPosition() + 1) % getNumColumns() == 1)) {
            return true;
        }
        return false;
    }

    private boolean isSelectedlastItem() {
        if (getNumColumns() == 1 || ((getSelectedItemPosition() + 1) % getNumColumns() == 0)) {
            return true;
        }
        return false;
    }

    public interface FocusGridViewColumnListener {

        void onColumnBegin();

        void onColumnEnd();
    }

    public void setOnGridViewColumnListener(FocusGridViewColumnListener listener){
        this.mGridViewColumnListener = listener;
    }
    @Override
    public boolean isInTouchMode() {
        if (19 <= Build.VERSION.SDK_INT) {
            return !(hasFocus() && !super.isInTouchMode());
        } else {
            return super.isInTouchMode();
        }
    }
}
