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.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wasu.cs.model.FocusEvent;

import cn.com.wasu.main.R;

/**
 * 电子竞技首页RecyclerView ->listView的item
 * Created by Danxingxi on 2016/3/30.
 */
public class ViewItemGroup extends LinearLayout {

    private RelativeLayout item1, item2, item3, item4,item5;
    private ImageView ivItem1,ivItem2,ivItem3,ivItem4;
    private TextView tvTap,tvMore;
    private LayoutInflater layoutInflater;

    public ViewItemGroup(Context context) {
        super(context);
        init(context);
    }

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

    public ViewItemGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context mContext) {

        layoutInflater = LayoutInflater.from(mContext);

        layoutInflater.inflate(R.layout.item_view_group, this);

        item1 = (RelativeLayout) findViewById(R.id.i1);
        item1.setTag(1);
        item2 = (RelativeLayout) findViewById(R.id.i2);
        item2.setTag(2);
        item3 = (RelativeLayout) findViewById(R.id.i3);
        item3.setTag(3);
        item4 = (RelativeLayout) findViewById(R.id.i4);
        item4.setTag(4);
        item5 = (RelativeLayout) findViewById(R.id.i5);
        item5.setTag(5);

        ivItem1 = (ImageView) findViewById(R.id.ivItem1);
        ivItem2 = (ImageView) findViewById(R.id.ivItem2);
        ivItem3 = (ImageView) findViewById(R.id.ivItem3);
        ivItem4 = (ImageView) findViewById(R.id.ivItem4);

        tvTap = (TextView) findViewById(R.id.tvTap);
        tvMore = (TextView) findViewById(R.id.tvMore);
    }

    /**
     * 如果调用肯定会在requestChildFocus()之前调用，获取上一次焦点的位置，确定这次焦点的位置
     * @param direction
     * @param previouslyFocusedRect
     * @return
     */
    @Override
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        int position = FocusEvent.getPosition();
        if(position > 0){
            if(position == 1){
                item1.requestFocus();
                return true;
            }else if(position == 2){
                item2.requestFocus();
                return true;
            }else if(position == 3){
                item3.requestFocus();
                return true;
            }else if(position == 4){
                item4.requestFocus();
                return true;
            }else if(position == 5){
                item5.requestFocus();
                return true;
            }
            else{
                return super.requestFocus(direction, previouslyFocusedRect);
            }
        }
        return super.requestFocus(direction, previouslyFocusedRect);
    }

    /**
     * 每次都会调用，调用时已经有item获取了焦点，这个时候可以记录焦点的位置
     * @param child
     * @param focused
     */
    @Override
    public void requestChildFocus(View child, View focused) {

        int position = Integer.valueOf(focused.getTag().toString());

        if(position > 0){
            FocusEvent.setPosition(position);
        }
        super.requestChildFocus(child, focused);
    }

    public void setData(){

    }
}
