package com.wasu.cs.ui;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.wasu.cs.mvp.IView.IDemandMvpView;
import com.wasu.cs.mvp.model.AssetsDataModel;
import com.wasu.cs.mvp.presenter.MainDemandPagePresenter;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.statistics.WasuStatistics;
import com.wasu.widgets.adapter.BaseFocusRecyclerViewAdapterTv;
import com.wasu.widgets.adapter.BaseFocusRecyclerViewHolderTv;
import com.wasu.widgets.focuswidget.IRecyclerView;
import com.wasu.widgets.focuswidget.ItemViewFocusSearchListener;
import com.wasu.widgets.focuswidget.TvRecyclerView;

import java.util.List;

import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;

/**
 * @Description: 新版首页点播屏
 * @Author: Danxingxi
 * @CreateDate: 2016/9/21 16:30
 */
public class MainDemandPageView extends ScrollView implements IDemandMvpView, BaseFocusRecyclerViewAdapterTv.OnItemListener
                                , ItemViewFocusSearchListener {

    private final static String TAG = "MainDemandPageView";
    private final static String PageName = "点播";

    private final static int MSG_RecycleImageView = 1;
    private final static int MSG_ShowImageView = 2;
    /**是否是第一次加载**/
    private boolean isFirst = false;
    /**imageView是否已经回收**/
    private boolean isRecycled = true;

    private final static int LEFT_PAGE = 1;
    private final static int RIGHT_PAGE = 3;

    private Context mContext;
    /**点播屏数据请求地址**/
    private String url = "http://120.26.137.228/?s=2002&p=sjctOndemand&k=1&v=3&type=19&subjectId=54&indCatId=222985";
    /**
     * 记录主动切屏时的焦点view
     */
    private View changePageFocusView;

    /**点播分类recyclerView**/
    private TvRecyclerView channelRecyclerView;
    /**第一排分类推荐recyclerView**/
    private TvRecyclerView oneRecyclerView;
    /**第二排分类推荐recyclerView**/
    private TvRecyclerView twoRecyclerView;
    /**第三排分类推荐recyclerView**/
    private TvRecyclerView threeRecyclerView;
    private TextView rowTitle01,rowTitle02,rowTitle03 ,rowNum01,rowNum02,rowNum03;

    private ChannelAdapter channelAdapter;
    private TvAdapter oneAdapter, twoAdapter, threeAdapter;

    private ViewPager parentViewPager;

    private MainDemandPagePresenter mainDemandPagePresenter;

    public MainDemandPageView(Context context, String url, ViewPager viewPager) {
        super(context);
        this.parentViewPager = viewPager;
        init(context, url);
    }

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

    public MainDemandPageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, url);
    }

    private void init(Context context, String url){
        this.mContext = context;
        this.url = url;
        setSmoothScrollingEnabled(true);
        setVerticalScrollBarEnabled(false);
        LayoutInflater.from(mContext).inflate(R.layout.main_demand_pageview, this);
        mainDemandPagePresenter = new MainDemandPagePresenter();
        mainDemandPagePresenter.attachView(this);

        findViewById();
        initListener();
    }

    private void findViewById(){

        channelRecyclerView = (TvRecyclerView) findViewById(R.id.channelRecyclerView);
        oneRecyclerView = (TvRecyclerView) findViewById(R.id.oneRecyclerView);
        twoRecyclerView = (TvRecyclerView) findViewById(R.id.twoRecyclerView);
        threeRecyclerView = (TvRecyclerView) findViewById(R.id.threeRecyclerView);

        rowTitle01 = (TextView) findViewById(R.id.rowTitle01);
        rowTitle02 = (TextView) findViewById(R.id.rowTitle02);
        rowTitle03 = (TextView) findViewById(R.id.rowTitle03);
        rowNum01 = (TextView) findViewById(R.id.rowNum01);
        rowNum02 = (TextView) findViewById(R.id.rowNum02);
        rowNum03 = (TextView) findViewById(R.id.rowNum03);

        channelAdapter  = new ChannelAdapter(channelRecyclerView);
        oneAdapter = new TvAdapter(1, oneRecyclerView);
        twoAdapter = new TvAdapter(2, twoRecyclerView);
        threeAdapter = new TvAdapter(3, threeRecyclerView);

    }

    private void initListener(){

        channelAdapter.setOnItemListener(this);
        oneAdapter.setOnItemListener(this);
        twoAdapter.setOnItemListener(this);
        threeAdapter.setOnItemListener(this);

        channelRecyclerView.setItemViewFocusSearchListener(this);
        oneRecyclerView.setItemViewFocusSearchListener(this);
        twoRecyclerView.setItemViewFocusSearchListener(this);
        threeRecyclerView.setItemViewFocusSearchListener(this);

        updateData();
    }
    private void updateData(){
        mainDemandPagePresenter.getDemandPageData(url);
    }

    /***
     * TvRecyclerView的itemView的焦点变化监听器
     * @param parent
     * @param itemView
     * @param position
     * @param hasFocus
     */
    @Override
    public void onItemFocusChanged(IRecyclerView parent, View itemView, int position, boolean hasFocus) {
        View maskView = itemView.findViewById(R.id.maskView);
        View tvName = itemView.findViewById(R.id.tvName);
        if(hasFocus){
            if(maskView != null){
                maskView.setVisibility(INVISIBLE);
            }
            if(tvName != null){
//                tvName.setVisibility(VISIBLE);
                tvName.setSelected(true);
            }
        }else{
            if(maskView != null){
                maskView.setVisibility(VISIBLE);
            }
            if(tvName != null){
//                tvName.setVisibility(INVISIBLE);
                tvName.setSelected(false);
            }
        }
        if(hasFocus){
            int id = ((TvRecyclerView)parent).getId();
            switch (id){
                case R.id.oneRecyclerView:
                    rowNum01.setText((position+1) + " / "+oneAdapter.getItemCount());
                    break;
                case R.id.twoRecyclerView:
                    rowNum02.setText((position+1) + " / "+twoAdapter.getItemCount());
                    break;
                case R.id.threeRecyclerView:
                    rowNum03.setText((position+1) + " / "+threeAdapter.getItemCount());
                    break;
            }
        }
    }

    /***
     * TvRecyclerView的itemView的点击监听器
     * @param parent
     * @param itemView
     * @param position
     */
    @Override
    public void onItemClick(IRecyclerView parent, View itemView, int position) {
        int id = ((TvRecyclerView)parent).getId();
        switch (id){
            case R.id.channelRecyclerView:
                AppUtil.playEnter = PageName+"_1_"+(position+1);
                WasuStatistics.getInstance().homeItemClick(parentViewPager.getCurrentItem(),"点播","1_"+(position+1),"",channelAdapter.getItemData(position).getTitle());
                IntentMap.startIntent(mContext,null, channelAdapter.getItemData(position).getLayout(), channelAdapter.getItemData(position).getJsonUrl(),null);
                break;
            case R.id.oneRecyclerView:
                AppUtil.playEnter = PageName+"_"+"2_"+(position+1);
                WasuStatistics.getInstance().click(String.valueOf(oneAdapter.getItemData(position).getId()));
                WasuStatistics.getInstance().homeItemClick(parentViewPager.getCurrentItem(),"点播","2_"+(position+1), String.valueOf(oneAdapter.getItemData(position).getId()), oneAdapter.getItemData(position).getTitle());
                IntentMap.startIntent(mContext,null, oneAdapter.getItemData(position).getLayout(), oneAdapter.getItemData(position).getJsonUrl(),null);
                break;
            case R.id.twoRecyclerView:
                AppUtil.playEnter = PageName+"_"+"3_"+(position+1);
                WasuStatistics.getInstance().click(String.valueOf(twoAdapter.getItemData(position).getId()));
                WasuStatistics.getInstance().homeItemClick(parentViewPager.getCurrentItem(),"点播","3_"+(position+1), String.valueOf(twoAdapter.getItemData(position).getId()),twoAdapter.getItemData(position).getTitle());
                IntentMap.startIntent(mContext,null, twoAdapter.getItemData(position).getLayout(), twoAdapter.getItemData(position).getJsonUrl(),null);
                break;
            case R.id.threeRecyclerView:
                AppUtil.playEnter = PageName+"_"+"4_"+(position+1);
                WasuStatistics.getInstance().click(String.valueOf(threeAdapter.getItemData(position).getId()));
                WasuStatistics.getInstance().homeItemClick(parentViewPager.getCurrentItem(),"点播","4_"+(position+1), String.valueOf(threeAdapter.getItemData(position).getId()),threeAdapter.getItemData(position).getTitle());
                IntentMap.startIntent(mContext,null, threeAdapter.getItemData(position).getLayout(), threeAdapter.getItemData(position).getJsonUrl(),null);
                break;
        }
    }

    ObjectAnimator animator;

    /**
     * 控制recyclerView的itemView焦点跳转
     * @param parent
     * @param itemView
     * @param itemCount
     * @param position
     * @param event
     * @return
     */
    @Override
    public boolean onItemViewFocusSearch(IRecyclerView parent, View itemView, int itemCount, int position, KeyEvent event) {
        //最后一排的数据标题做处理
        if( event.getAction() != KeyEvent.ACTION_UP &&(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP ||  event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN)){
            int id = ((TvRecyclerView)parent).getId();
            switch (id){
                case R.id.oneRecyclerView:
                    rowNum01.setText("0 / "+oneAdapter.getItemCount());
                    break;
                case R.id.twoRecyclerView:
                    rowNum02.setText("0 / "+twoAdapter.getItemCount());
                    break;

            }
        }
        //最后一排的数据标题做区别处理
        if( event.getAction() != KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP){
            int id = ((TvRecyclerView)parent).getId();
            if( R.id.threeRecyclerView == id)
                rowNum03.setText("0 / "+threeAdapter.getItemCount());
        }
        int id = ((TvRecyclerView)parent).getId();
        if (id == R.id.channelRecyclerView) {  //对第一排有跨度的recyclerView做特殊处理
            if ((position + 1 == itemCount || position + 2 == itemCount) && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT
                    && event.getAction() != KeyEvent.ACTION_UP) {
//                    parent.shockAnim(itemView);
//                /**为乐视应用市场暂时屏蔽生活屏，点播屏禁右翻页**/
                changeViewPager(RIGHT_PAGE);
                return true;
            }
            if (position == 0 && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT && event.getAction() != KeyEvent.ACTION_UP) {
//                animator = parent.shockAnim(itemView);
                changeViewPager(LEFT_PAGE);
                return true;
            }
            return false;
        } else { //非第一排的recyclerView做处理

            if (position + 1 == itemCount && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() != KeyEvent.ACTION_UP) {
//                parent.shockAnim(itemView);
//                /**为乐视应用市场暂时屏蔽生活屏，点播屏禁右翻页**/
                changeViewPager(RIGHT_PAGE);
                return true;
            }
            if (position == 0 && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT && event.getAction() != KeyEvent.ACTION_UP) {
//                animator = parent.shockAnim(itemView);
                //为了显示一个数字很小的功能，不得不加上这些傻逼一样的代码
                switch (id){
                    case R.id.oneRecyclerView:
                        rowNum01.setText("0 / "+oneAdapter.getItemCount());
                        break;
                    case R.id.twoRecyclerView:
                        rowNum02.setText("0 / "+twoAdapter.getItemCount());
                        break;
                    case R.id.threeRecyclerView:
                        rowNum03.setText("0 / "+threeAdapter.getItemCount());
                        break;
                }
                changeViewPager(LEFT_PAGE);
                return true;
            }

        }
        return false;
    }

    /**
     * 主动使ViewPager翻页
     * @param viewPagerPosition
     */
    public void changeViewPager(int viewPagerPosition){
        if(parentViewPager != null){
            changePageFocusView = findFocus();
            if(viewPagerPosition == LEFT_PAGE){
                parentViewPager.setCurrentItem(viewPagerPosition, true);
            }else{
                parentViewPager.setCurrentItem(viewPagerPosition, true);
            }
        }
    }

    @Override
    public void scrollToTop() {
        fullScroll(ScrollView.FOCUS_UP);
    }

    /**
     * 修改scrollview的滚动速度
     * @param velocityY
     */
    @Override
    public void fling(int velocityY) {
        super.fling(velocityY*8);
    }

    /**
     * 第一排数据获取成功
     *
     * @param bodyListData
     **/
    @Override
    public void getBody1DataSuccess(String rowTitle, List<AssetsDataModel> bodyListData) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(0==position || 1==position){
                    return 2;
                }
                return 1;
            }
        });

        channelRecyclerView.setLayoutManager(gridLayoutManager);
        channelRecyclerView.setAdapter(channelAdapter);
        channelAdapter.setData(bodyListData);

    }

    /**
     * 第二排数据获取成功
     *
     * @param bodyListData
     **/
    @Override
    public void getBody2DataSuccess(String rowTitle, List<AssetsDataModel> bodyListData) {
        LinearLayoutManager LinearLayoutManager = new LinearLayoutManager(mContext);
        LinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        oneRecyclerView.setLayoutManager(LinearLayoutManager);

        oneRecyclerView.setAdapter(oneAdapter);
        rowTitle01.setText(rowTitle);
        oneAdapter.setData(bodyListData);
        rowNum01.setText("0 / "+oneAdapter.getItemCount());

    }

    /**
     * 第三排数据获取成功
     *
     * @param bodyListData
     **/
    @Override
    public void getBody3DataSuccess(String rowTitle, List<AssetsDataModel> bodyListData) {
        LinearLayoutManager LinearLayoutManager2 = new LinearLayoutManager(mContext);
        LinearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        twoRecyclerView.setLayoutManager(LinearLayoutManager2);

        twoRecyclerView.setAdapter(twoAdapter);
        rowTitle02.setText(rowTitle);
        twoAdapter.setData(bodyListData);
        rowNum02.setText("0 / "+twoAdapter.getItemCount());

    }

    @Override
    public void getBody4DataSuccess(String rowTitle, List<AssetsDataModel> bodyListData) {
        LinearLayoutManager LinearLayoutManager3 = new LinearLayoutManager(mContext);
        LinearLayoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL);
        threeRecyclerView.setLayoutManager(LinearLayoutManager3);

        threeRecyclerView.setAdapter(threeAdapter);
        rowTitle03.setText(rowTitle);
        threeAdapter.setData(bodyListData);
        rowNum03.setText("0 / "+threeAdapter.getItemCount());
    }


    /**
     * 获取数据失败
     *
     * @param e
     */
    @Override
    public void getDataError(Throwable e) {

    }

    /**
     * 显示loading
     */
    @Override
    public void showProgress() {

    }

    /**
     * 隐藏loading
     */
    @Override
    public void hideProgress() {

    }

    /**
     * 第一次请求焦点
     * @param direction
     */
    public void firstRequestFocus(int direction ){
        onRequestFocusInDescendants(direction ,null);
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
        if(previouslyFocusedRect == null) {
            if (direction == View.FOCUS_DOWN) {
                channelRecyclerView.requestFocus(direction);
                return true;
            }else if( 1 == AppUtil.MAIN_PAGERVIEW_POSITION ){
                if(channelRecyclerView!=null && channelRecyclerView.getChildAt(0)!=null){
                    scrollTo(0, 0);
                    channelRecyclerView.getChildAt(0).requestFocus();
                    return true;
                }
            }else if( 3 == AppUtil.MAIN_PAGERVIEW_POSITION ){
                if(channelRecyclerView!=null && channelRecyclerView.getChildCount()>0){
                    scrollTo(0, 0);
                    int childCount = channelRecyclerView.getChildCount();
                    if(childCount>2 && 0 == childCount%2){
                        View view = channelRecyclerView.getChildAt(childCount-2);
                        if(view != null){
                            view.requestFocus();
                        }
                    }else{
                        View view = channelRecyclerView.getChildAt(childCount-1);
                        if(view != null){
                            view.requestFocus();
                        }
                    }
                    return true;
                }
            }
        }
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }

    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
        if (getChildCount() == 0)
            return 0;
        int height = getHeight();
        int screenTop = getScrollY();
        int screenBottom = screenTop + height;
        int fadingEdge = this.getResources().getDimensionPixelSize(R.dimen.d_160dp);
        if (rect.top > 0) {
            screenTop += fadingEdge;
        }
        if (rect.bottom < getChildAt(0).getHeight()) {
            screenBottom -= fadingEdge;
        }
        //
        int scrollYDelta = 0;
        if (rect.bottom > screenBottom && rect.top > screenTop) {
            if (rect.height() > height) {
                scrollYDelta += (rect.top - screenTop);
            } else {
                scrollYDelta += (rect.bottom - screenBottom);
            }
            int bottom = getChildAt(0).getBottom();
            int distanceToBottom = bottom - screenBottom;
            scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
        } else if (rect.top < screenTop && rect.bottom < screenBottom) {
            if (rect.height() > height) {
                scrollYDelta -= (screenBottom - rect.bottom);
            } else {
                scrollYDelta -= (screenTop - rect.top);
            }
            scrollYDelta = Math.max(scrollYDelta, -getScrollY());
        }
        return scrollYDelta;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mainDemandPagePresenter != null){
            mainDemandPagePresenter.detachView();
            mainDemandPagePresenter = null;
        }
    }

    /***********************************************************************************************/
    class ChannelAdapter extends BaseFocusRecyclerViewAdapterTv<AssetsDataModel> {

        public ChannelAdapter(TvRecyclerView recyclerView) {
            super(recyclerView);
        }

        /**
         * 创建item view
         *
         * @param parent
         * @param viewType
         * @return
         */
        @Override
        protected BaseFocusRecyclerViewHolderTv createItem(ViewGroup parent, int viewType) {
            View view  = LayoutInflater.from(mContext).inflate(R.layout.item_home_recom_span, null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        /**
         * 绑定数据
         *
         * @param holder
         * @param position
         */
        @Override
        protected void bindData(BaseFocusRecyclerViewHolderTv holder, int position) {
//            ImageFetcherModule.getInstance().attachImage(getItemData(position).getPicUrl(), ((MyViewHolder)holder).ivPic);
            holder.itemView.setTag(getItemData(position).getPicUrl());
            if(!isFirst)
            FrescoImageFetcherModule.getInstance().attachImage(getItemData(position).getPicUrl(), ((MyViewHolder)holder).ivPic);
            ((MyViewHolder) holder).itemView.setContentDescription(getItemData(position).getTitle());
        }

        class MyViewHolder extends BaseFocusRecyclerViewHolderTv {
            ImageView ivPic;
            /**
             * 创建FocusRecyclerView的itemView
             *
             * @param itemView          item View
             */
            public MyViewHolder(View itemView) {
                super(itemView, getItemParent(), getItemListener());
                ivPic = (ImageView) itemView.findViewById(R.id.ivPic);
            }

            @Override
            protected View getView() {
                return null;
            }
        }
    }
    /***********************************************************************************************/
    class TvAdapter extends BaseFocusRecyclerViewAdapterTv<AssetsDataModel> {
        int rowPosition = 0;

        public TvAdapter(int position, TvRecyclerView tvRecyclerView){
            super(tvRecyclerView);
            this.rowPosition = position;
        }

        /**
         * 创建item view
         *
         * @param parent
         * @param viewType
         * @return
         */
        @Override
        protected BaseFocusRecyclerViewHolderTv createItem(ViewGroup parent, int viewType) {
            View view  = LayoutInflater.from(mContext).inflate(R.layout.item_home_demand, null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        /**
         * 绑定数据
         *
         * @param holder
         * @param position
         */
        @Override
        protected void bindData(BaseFocusRecyclerViewHolderTv holder, int position) {
            switch (rowPosition){
                case 1:
                    holder.itemView.setTag(oneAdapter.getItemData(position).getPicUrl());
                    if(!isFirst)
                    FrescoImageFetcherModule.getInstance().attachImage(oneAdapter.getItemData(position).getPicUrl(), ((MyViewHolder)holder).ivPic);
                    ((MyViewHolder)holder).tvName.setText(oneAdapter.getItemData(position).getTitle());
                    break;
                case 2:
                    holder.itemView.setTag(twoAdapter.getItemData(position).getPicUrl());
                    if(!isFirst)
                    FrescoImageFetcherModule.getInstance().attachImage(twoAdapter.getItemData(position).getPicUrl(), ((MyViewHolder)holder).ivPic);
                    ((MyViewHolder)holder).tvName.setText(twoAdapter.getItemData(position).getTitle());
                    break;
                case 3:
                    holder.itemView.setTag(threeAdapter.getItemData(position).getPicUrl());
                    if(!isFirst)
                    FrescoImageFetcherModule.getInstance().attachImage(threeAdapter.getItemData(position).getPicUrl(), ((MyViewHolder)holder).ivPic);
                    ((MyViewHolder)holder).tvName.setText(threeAdapter.getItemData(position).getTitle());
                    break;
            }
        }

        class MyViewHolder extends BaseFocusRecyclerViewHolderTv{
            public ImageView ivPic;
            public TextView tvName;
            /**
             * 创建FocusRecyclerView的itemView
             *
             * @param itemView          item View
             */
            public MyViewHolder(View itemView) {
                super(itemView, getItemParent(), getItemListener());
                ivPic = (ImageView) itemView.findViewById(R.id.ivPic);
                tvName = (TextView) itemView.findViewById(R.id.tvName);
            }

            @Override
            protected View getView() {
                return null;
            }
        }
    }
    /***********************************************************************************************/
    /**
     * 回收和显示页面上的图片资源，减少内存占用
     */
    final Handler imageViewHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            int what = msg.what;
            if(MSG_RecycleImageView == what){
                recycleImageViewTask();
            }else if(MSG_ShowImageView == what){
                showImageViewTask();
            }
            return false;
        }
    });

    public void recycleImageView(){
//        imageViewHandler.removeMessages(MSG_ShowImageView);
//        imageViewHandler.removeMessages(MSG_RecycleImageView);
//        if(isRecycled)
//            return;
//        imageViewHandler.sendEmptyMessageDelayed(MSG_RecycleImageView, 600);
    }

    public void showImageView(){
//        imageViewHandler.removeMessages(MSG_ShowImageView);
//        imageViewHandler.removeMessages(MSG_RecycleImageView);
//        if(!isRecycled)
//            return;
//        imageViewHandler.sendEmptyMessageDelayed(MSG_ShowImageView, 600);
    }

    private void recycleImageViewTask(){
        if(channelRecyclerView != null && channelRecyclerView.getChildCount()>0){
            int itemCount = channelRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = channelRecyclerView.getChildAt(i);
                if(itemView != null){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    if(imageView != null){
                        imageView.setImageBitmap(null);
                    }
                }
            }
        }
        if(oneRecyclerView != null && oneRecyclerView.getChildCount()>0){
            int itemCount = oneRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = oneRecyclerView.getChildAt(i);
                if(itemView != null){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    if(imageView != null){
                        imageView.setImageBitmap(null);
                    }
                }
            }
        }
        if(twoRecyclerView != null && twoRecyclerView.getChildCount()>0){
            int itemCount = twoRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = twoRecyclerView.getChildAt(i);
                if(itemView != null){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    if(imageView != null){
                        imageView.setImageBitmap(null);
                    }
                }
            }
        }
        if(threeRecyclerView != null && threeRecyclerView.getChildCount()>0){
            int itemCount = threeRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = threeRecyclerView.getChildAt(i);
                if(itemView != null){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    if(imageView != null){
                        imageView.setImageBitmap(null);
                    }
                }
            }
        }
        isRecycled = true;
    }

    private void showImageViewTask(){
        isFirst = false;
        if(channelRecyclerView != null && channelRecyclerView.getChildCount()>0){
            int itemCount = channelRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = channelRecyclerView.getChildAt(i);
                String url = itemView.getTag().toString();
                if(itemView != null && url != null && !TextUtils.isEmpty(url)){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    FrescoImageFetcherModule.getInstance().attachImage(url, imageView);
                }
            }
        }
        if(oneRecyclerView != null && oneRecyclerView.getChildCount()>0){
            int itemCount = oneRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = oneRecyclerView.getChildAt(i);
                String url = itemView.getTag().toString();
                if(itemView != null && url != null && !TextUtils.isEmpty(url)){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    FrescoImageFetcherModule.getInstance().attachImage(url, imageView);
                }
            }
        }
        if(twoRecyclerView != null && twoRecyclerView.getChildCount()>0){
            int itemCount = twoRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = twoRecyclerView.getChildAt(i);
                String url = itemView.getTag().toString();
                if(itemView != null && url != null && !TextUtils.isEmpty(url)){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    FrescoImageFetcherModule.getInstance().attachImage(url, imageView);
                }
            }
        }
        if(threeRecyclerView != null && threeRecyclerView.getChildCount()>0){
            int itemCount = threeRecyclerView.getChildCount();
            for(int i=0;i<itemCount;i++){
                View itemView = threeRecyclerView.getChildAt(i);
                String url = itemView.getTag().toString();
                if(itemView != null && url != null && !TextUtils.isEmpty(url)){
                    ImageView imageView = (ImageView) itemView.findViewById(R.id.ivPic);
                    FrescoImageFetcherModule.getInstance().attachImage(url, imageView);
                }
            }
        }
        isRecycled = false;
    }

}
