package com.wasu.cs.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wasu.cs.model.CatData.BaseElement;
import com.wasu.cs.model.CatData.Element;
import com.wasu.cs.model.CatData.AssetElement;
import com.wasu.cs.ui.ActivityPlayer;

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

/**
 * Created by LiuYang on 7/22/15.
 */
public class AssetListLayout extends LinearLayout implements IViewItem, View.OnClickListener {

    private int mSelectIndex = 0;
    private View mSelectedView = null;
    private boolean isSelected = false;
    
    public AssetListLayout(Context context) {
        super(context);
        init(context);
    }

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

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

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.item_asset_list_layout, this, false);
        setOrientation(LinearLayout.VERTICAL);
        setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setClickable(true);
        setBackgroundResource(R.drawable.asset_list_corner_bg);
        setOnClickListener(this);
    }

    @Override
    public void setData(BaseElement data) {
        removeAllViews();
        Element element = (Element)data;
        int size = element.getAssets().size();
        if(size > 6) {
            size = 6;
        }
        for(int i = 0;i < size;i++) {
            AssetElement assetElement = element.getAssets().get(i);
            AssetListItem listItem = new AssetListItem(getContext());
            listItem.setData(assetElement);
            listItem.setIndex(i);
            listItem.setJsonUrl(data.getJsonUrl());
            listItem.setCount(size);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

            addView(listItem, params);
        }
    }

    @Override
    public void setSelected(boolean selected) {
        isSelected = selected;
        if(selected) {
            setSelectItem(mSelectIndex);
        }
        else {
           if(getChildAt(mSelectIndex) != null){
        	   getChildAt(mSelectIndex).setSelected(false);
           }
        }
    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    private void setSelectItem(int index) {
        if(mSelectedView != null) {
            mSelectedView.setSelected(false);
        }

        mSelectedView = getChildAt(index);
        if(mSelectedView != null)
            mSelectedView.setSelected(true);
    }

    @Override
    public View focusSearch(int direction) {
        switch (direction) {
            case View.FOCUS_DOWN:
                if(mSelectIndex < getChildCount() - 1) {
                    mSelectIndex++;
                    setSelectItem(mSelectIndex);
                    return this;
                }
                break;

            case View.FOCUS_UP:
                if(mSelectIndex > 0) {
                    mSelectIndex--;
                    setSelectItem(mSelectIndex);
                    return this;
                }
                break;
        }

        return super.focusSearch(direction);
    }

    @Override
    public void onClick(View view) {
        if(mSelectedView != null) {
            mSelectedView.performClick();
        }
    }

	public class AssetListItem extends LinearLayout implements OnClickListener,IViewItem {

        private TextView mTextView;
        
        private int index;
        private String jsonUrl;
        private int count;

		public void setIndex(int index) {
			this.index = index;
		}

        public void setJsonUrl(String jsonUrl) {
			this.jsonUrl = jsonUrl;
		}

		public void setCount(int count) {
			this.count = count;
		}

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

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

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

        private void init(Context context) {
            LayoutInflater.from(context).inflate(R.layout.item_asset_list_item,this);
            mTextView = (TextView) findViewById(R.id.name);
            setOnClickListener(this);
        }

        @Override
        public void setData(BaseElement element) {
            mTextView.setText(element.getName());
        }
        
		@Override
        public void onClick(View view) {
            //IntentMap.startIntent(getContext(), null, mData.getLayout(), mData.getJsonUrl(), null);
        	
        	Intent intent = new Intent();
			intent.putExtra(IntentConstant.PLAY_TYPE.value(), ActivityPlayer.PLAY_TYPE_LOOP);
			intent.putExtra(IntentConstant.PLAY_INDEX.value(), index);
			intent.putExtra(ActivityPlayer.ASSET_URL, jsonUrl + "&page=1&psize=" + count);
//			intent.putExtra(ActivityPlayer.ASSET_URL, jsonUrl);
			IntentMap.startIntent(getContext(), intent, null, null, ActivityPlayer.class);
        }
    }
}
