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

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

import org.json.JSONException;
import org.json.JSONObject;

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

/**
 * Created by LiuYang on 7/17/15.
 */
public class ChannelNewsListItem extends RelativeLayout implements View.OnClickListener,IViewItem{

    private TextView mTextView;
    private ImageView mImageView;
    private AssetElement mData;

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

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

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

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.item_channel_news_list, this);

//        setFocusable(true);
//        setFocusableInTouchMode(true);
//        setClickable(true);

        mTextView = (TextView) findViewById(R.id.name);
        mImageView = (ImageView) findViewById(R.id.ivPic);

        setOnClickListener(this);
    }

    @Override
    public void setData(BaseElement element) {
        this.mData = (AssetElement) element;
        mTextView.setText(mData.getTitle());
        FrescoImageFetcherModule.getInstance().attachImage(element.getPicUrl(), mImageView,getResources().getDimensionPixelSize(R.dimen.d_3dp));
    }

    @Override
    public void onClick(View view) {
    	try {
			JSONObject json = new JSONObject((String) view.getTag());
			
			Intent intent = new Intent();
			intent.putExtra(IntentConstant.PLAY_TYPE.value(), ActivityPlayer.PLAY_TYPE_LOOP);
			intent.putExtra(IntentConstant.PLAY_INDEX.value(), json.getInt("playIndex"));
			intent.putExtra(IntentConstant.DATAURI.value(), json.getString("jsonUrl"));
			
			IntentMap.startIntent(getContext(), intent, null, null, ActivityPlayer.class);
		} catch (JSONException e) {
			e.printStackTrace();
		}
    }
}
