package com.wasu.cs.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.wasu.cs.widget.MainPageLiveVideoView;
import com.wasu.cs.widget.ViewItemGroup;
import com.wasu.module.log.WLog;
import com.wasu.widgets.adapter.BaseFocusRecyclerViewAdapter;
import com.wasu.widgets.adapter.BaseFocusRecyclerViewHolder;
import com.wasu.widgets.focuswidget.FocusLinearLayout;
import com.wasu.widgets.focuswidget.FocusRecyclerView;

import java.util.ArrayList;

import cn.com.wasu.main.R;

/**
 * Created by Danxx on 2016/3/29.
 */
public class ActivityTest extends Activity{
    private String TAG = "ActivityTest";

    private Button buttn;
    private RecyclerView vRecyclerView;
    private ArrayList<Integer> mData = new ArrayList<Integer>();
    private VRecyclerViewAdapter vRecyclerViewAdapter;

    private MainPageLiveVideoView liveVideoView;
    private FocusLinearLayout focusLayout;

    private FocusRecyclerView recomRecyclerView;

    ArrayList<String> data = new ArrayList<>();

    MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WLog.i(TAG,"onCreate()");
//        MainRecomPageView mainRecomPageView = new MainRecomPageView(ActivityTest.this, "" , null);


        MainDemandPageView mainDemandPageView = new MainDemandPageView(ActivityTest.this, "" , null);

        setContentView(new MainLifePageView(ActivityTest.this));


/*
        for(int i=0;i<6;i++){
            data.add("Data:"+i);
        }

        liveVideoView  = (MainPageLiveVideoView) findViewById(R.id.liveVideoView);

        focusLayout = (FocusLinearLayout) findViewById(R.id.focusLayout);

        focusLayout.setItemFocusChangeListener(new FocusLinearLayout.onItemFocusChangeListener() {
            @Override
            public void onItemFocusChange(View itemView, int position, boolean hasFocus) {
                if(1 == position){
                    if(hasFocus){
                        liveVideoView.onFocused();
                        WLog.d("danxx", "视频窗口获得焦点哦23333");
                    }else {
                        WLog.d("danxx", "视频窗口失去焦点哦32222");
                        liveVideoView.onUnFocused();
                    }
                }
            }
        });

        recomRecyclerView = (FocusRecyclerView) findViewById(R.id.changeRecyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recomRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MyAdapter(recomRecyclerView);
        mAdapter.setData(data);
        recomRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = getResources().getDimensionPixelSize(cn.com.wasu.main.R.dimen.d_16dp);

            }
        });
        recomRecyclerView.setAdapter(mAdapter);*/


//        FocusLinearLayout relativeLayout = (FocusLinearLayout) findViewById(R.id.focus_rl);

//        WLog.d("danxx", "ActivityTest onCreate");
//
//        buttn = (Button) findViewById(R.id.buttn);
//        vRecyclerView  = (RecyclerView) findViewById(R.id.vRecyclerView);
//
//        //设置布局管理器
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        vRecyclerView.setLayoutManager(linearLayoutManager);
//
//        for (int i=0;i<30;i++){
//            mData.add(i);
//        }
//
//        vRecyclerViewAdapter = new VRecyclerViewAdapter(ActivityTest.this,mData);
//
//        vRecyclerView.setAdapter(vRecyclerViewAdapter);
//
//        buttn.setNextFocusDownId(R.id.vRecyclerView);
//
//        vRecyclerView.setNextFocusUpId(R.id.buttn);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    class MyAdapter extends BaseFocusRecyclerViewAdapter<String>{

        public MyAdapter(FocusRecyclerView recyclerView) {
            super(recyclerView);
        }

        /**
         * 创建item view
         *
         * @param parent
         * @param viewType
         * @return
         */
        @Override
        protected BaseFocusRecyclerViewHolder createItem(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(ActivityTest.this).inflate(R.layout.item_home_recom_header, null);
            MyViewHolder myViewHolder = new MyViewHolder(view, getItemParent(), getItemListener());
            return myViewHolder;
        }

        /**
         * 绑定数据
         *
         * @param holder
         * @param position
         */
        @Override
        protected void bindData(BaseFocusRecyclerViewHolder holder, int position) {
            if(5 == position){
                ((MyViewHolder)holder).ivPic.setImageResource(R.drawable.change);
            }
        }

        class MyViewHolder extends BaseFocusRecyclerViewHolder{

            public ImageView ivPic;

            /**
             * 创建FocusRecyclerView的itemView
             *
             * @param itemView          item View
             * @param focusRecyclerView item parent ViewGroup(Parent must instanceof FocusRecyclerView)
             * @param mOnItemListener   item click、longClick、focusChange listener
             */
            public MyViewHolder(View itemView, FocusRecyclerView focusRecyclerView, OnItemListener mOnItemListener) {
                super(itemView, focusRecyclerView, mOnItemListener);
                ivPic = (ImageView) itemView.findViewById(R.id.ivPic);
            }

            @Override
            protected View getView() {
                return null;
            }
        }
    }


    class VRecyclerViewAdapter extends RecyclerView.Adapter<VRecyclerViewAdapter.ViewHolder>{

        private Context mContext;
        private ArrayList<Integer> mData;
        private LayoutInflater mInflater;

        public VRecyclerViewAdapter(Context mContext, ArrayList<Integer> mData) {
            this.mContext = mContext;
            this.mData = mData;
            mInflater = LayoutInflater.from(mContext);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int i) {
//            viewHolder.getmView().setData();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = new ViewItemGroup(ActivityTest.this);
            ViewHolder viewHolderv= new ViewHolder(view);
            return viewHolderv;
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public VRecyclerViewAdapter() {
            super();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            private ViewItemGroup mView;
            public ViewHolder(View view) {
                super(view);
                mView = (ViewItemGroup) view;
                mView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
                mView.setFocusable(true);
                mView.setClickable(true);
            }

            public ViewItemGroup getmView() {
                return mView;
            }
        }
    }
}
