package com.wasu.cs.widget;

import java.lang.reflect.Field;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.AbsListView;
import android.widget.ImageView;

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

import com.wasu.widget.HeaderGridView;
import com.wasu.widgets.focuswidget.CustomFocusGridView;

public class HeaderGridViewEx extends HeaderGridView{
	private static final String TAG = "HeaderGridViewEx";
	private GridViewColumnBeginListener mGridViewColumnBeginListener;

	public HeaderGridViewEx(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public HeaderGridViewEx(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		resetScrollBar();
		setContentDescription("内容信息列表");
	}

	/**
	 * 修改scrollbar样式
	 */
	@SuppressLint("NewApi")
	private void resetScrollBar() {
		try {
			Field mFastScroller = AbsListView.class.getDeclaredField("mFastScroller");
			mFastScroller.setAccessible(true);
			Object o = mFastScroller.get(this);
			if (Build.VERSION.SDK_INT < 19) { //4.4以下
				// 滑动指示器
				Field mThumbDrawable = mFastScroller.getType().getDeclaredField("mThumbDrawable");
				mThumbDrawable.setAccessible(true);
				Drawable thumbDrawable = (Drawable) mThumbDrawable.get(o);
				thumbDrawable = getResources().getDrawable(R.drawable.scrollbar_drawable_vertical_thumb);
				mThumbDrawable.set(o, thumbDrawable);
				// 滑动指示器宽度
				Field mThumbWidth = mFastScroller.getType().getDeclaredField("mThumbW");
				mThumbWidth.setAccessible(true);
				int thumbWidth = (Integer) mThumbWidth.get(o);
				thumbWidth = getResources().getDimensionPixelSize(R.dimen.d_10dp);
				mThumbWidth.set(o, thumbWidth);
				//滑动指示器高度
				Field mThumbHeight = mFastScroller.getType().getDeclaredField("mThumbH");
				mThumbHeight.setAccessible(true);
				int thumbHeight = (Integer) mThumbHeight.get(o);
				thumbHeight = getResources().getDimensionPixelSize(R.dimen.d_90dp);;
				mThumbHeight.set(o, thumbHeight);
			} else {
				// 滑动指示器
				Field mThumbImage = mFastScroller.getType().getDeclaredField("mThumbImage");
				mThumbImage.setAccessible(true);
				ImageView thumbImage = (ImageView) mThumbImage.get(o);
				thumbImage.setBackground(getResources().getDrawable(R.drawable.scrollbar_drawable_vertical_thumb));
				mThumbImage.set(o, thumbImage);
				// 背景
				Field mTrackImage = mFastScroller.getType().getDeclaredField("mTrackImage");
				mTrackImage.setAccessible(true);
				ImageView trackImage = (ImageView) mTrackImage.get(o);
				trackImage.setBackground(getResources().getDrawable(R.drawable.scrollbar_drawable_vertical_track));
				mTrackImage.set(o, trackImage);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void repairPadding(){
		try {
			Field mSelectedTop = AbsListView.class.getDeclaredField("mSelectedTop");
			mSelectedTop.setAccessible(true);
			int top = mSelectedTop.getInt(this);
			top += getPaddingTop();
			mSelectedTop.setInt(this, top);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		//针对乐视的渠道进行焦点的特殊处理
		if (BuildConfig.FLAVOR.equalsIgnoreCase(ChannelFlavor.LETV_MARKET)) {
			if (1 == ((getSelectedItemPosition() + 1) % getNumColumns()) && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
				if (null != mGridViewColumnBeginListener) {
					mGridViewColumnBeginListener.onColumnBegin();
				}
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);
	}
	public interface GridViewColumnBeginListener {
		void onColumnBegin();
	}

	public void setOnGridViewColumnBeginListener(GridViewColumnBeginListener listener) {
		this.mGridViewColumnBeginListener = listener;
	}
}
