package com.wasu.cs.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.wasu.cs.model.DemandProgram;

import java.util.ArrayList;

import cn.com.wasu.main.R;

public class DetailSeriesSetView extends LinearLayout {
	private static final String TAG = "DetaialseriesSetView";
	ItemChangedListner listner;
	View[] mViewItems;
	Context context;
	DemandProgram mDemandProgram;
    boolean mOpenKey = true;
	/**
	 * 采用Runnable检测的方法，用于适配小米盒子出现的焦点丢失问题
	 * */
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			removeCallbacks(runnable);

			if (getChildAt(index).hasFocus()) {
				setItemFocused(index);
			} else {
				setItemSelected(index);
			}
		}
	};

    
	public DetailSeriesSetView(Context context) {
		super(context);
		this.context=context;
	}
	public DetailSeriesSetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
	}
	public DetailSeriesSetView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context=context;
	}
	int index=0;
	public void  setIndex(int index){
		this.index=index;
	}
	public int getIndex(){
		return index;
	}
	public void changePage(int pagenum){
		setItemNormal(index);
		setItemSelected(pagenum);
		setIndex(pagenum);
	}
	public void setItemSelected(int index){
		((Button)mViewItems[index].findViewById(R.id.item_detail_series_set_text)).setTextColor(context.getResources().getColor(R.color.white));
		(mViewItems[index].findViewById(R.id.item_detail_series_set_divide)).setVisibility(View.VISIBLE);
	}
	public void setItemFocused(int index){
		((Button)mViewItems[index].findViewById(R.id.item_detail_series_set_text)).setTextColor(context.getResources().getColor(R.color.yellow));
		(mViewItems[index].findViewById(R.id.item_detail_series_set_divide)).setVisibility(View.VISIBLE);
	}
	public void setItemNormal(int index){
		((Button)mViewItems[index].findViewById(R.id.item_detail_series_set_text)).setTextColor(context.getResources().getColor(R.color.textcolor_detail));
		(mViewItems[index].findViewById(R.id.item_detail_series_set_divide)).setVisibility(View.INVISIBLE);
	}
	public void setItemNum(int num,int seriestotal){
		mViewItems=new View[num];
		for(int i=0;i<num;i++){
			mViewItems[i]=LayoutInflater.from(context).inflate(R.layout.item_detail_series_set, null);
			Button nameTextView=(Button)mViewItems[i].findViewById(R.id.item_detail_series_set_text);
			int from=mDemandProgram.getDetailSeriesSet().getSerieslist().get(i*30);
			//最后一页做判断,如果不够30个
			int to;
			if((i+1)*30>=mDemandProgram.getDetailSeriesSet().getSize()){
				to=mDemandProgram.getDetailSeriesSet().getSerieslist().get(mDemandProgram.getDetailSeriesSet().getSize()-1);
			}else{
				to=mDemandProgram.getDetailSeriesSet().getSerieslist().get((i+1)*30-1);
			}
			nameTextView.setText(from+"-"+to);
			nameTextView.setOnFocusChangeListener(new seriesSetFocus(i));
			addView(mViewItems[i]);
		}
	}
	
	private class seriesSetFocus implements OnFocusChangeListener{
		int index=0;
		public seriesSetFocus(int i) {
			this.index=i;
		}
		@Override
		public void onFocusChange(View v, boolean hasFocus) {

			if(hasFocus==true&&listner!=null){//获得焦点
				postDelayed(runnable, 200);

				listner.ItemChanged(index);
				setItemFocused(index);
			}else{
				if(getDirection()==FOCUS_UP){//焦点上去
					setIndex(index);
					setItemSelected(index);
				}else if(getDirection()==FOCUS_RIGHT||getDirection()==FOCUS_LEFT){
                    if (mOpenKey == true) {
                        setItemNormal(index);//焦点平移
                        mOpenKey = false;
                    } else {
                        mOpenKey = true;
                    }
                } else {
                }
			}
		}
	}
	@Override
	public void addFocusables(ArrayList<View> views, int direction,
			int focusableMode) {
		this.direction=direction;
		if(hasFocus()&&direction==FOCUS_DOWN){
			getChildAt(index).addFocusables(views, direction,focusableMode);
		}else{
			super.addFocusables(views, direction, focusableMode);
			
		}
	}
	int direction=0;
	public int getDirection(){
		return direction;
	}
	
	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
		
	}
	@Override
	public boolean dispatchUnhandledMove(View focused, int direction) {
		return true;
	}
	@Override
	protected boolean onRequestFocusInDescendants(int direction,
			Rect previouslyFocusedRect) {
		super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
		return true;
	}
	@Override
	public boolean hasFocus() {
		return super.hasFocusable();
	}
	
	public void setItemChangedListner(ItemChangedListner listner){
		this.listner=listner;
	}
	public interface ItemChangedListner{
		public void ItemChanged(int index);
	}
	public void setDemandProgram(DemandProgram mDemandProgram) {
		this.mDemandProgram=mDemandProgram;
		
	}
}
