package com.wasu.cs.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.model.DemandSeries;
import com.wasu.cs.widget.ChannelListView;
import com.wasu.cs.widget.ColumnDetailListItem;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.module.datafetch.DataFetchListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.module.datafetch.ObjectBase;
import com.wasu.module.log.WLog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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

/**
 * Created by mateng on 8/6/15.
 */
public class ActivityColumnDetail extends ActivityBase {
    private String TAG = "ActivityColumnDetail";

    private Context context;

    private ColumnDetailAdapter mAdapter;
    private TextView mTitleView;
    private ChannelListView mListView;
    private ImageView mBgImageView;
    private List<DemandSeries> mItemList;

    private DemandProgram mAssetDetailData;

    private String JSON_URL = "http://121.40.195.74/?s=2002&p=sntAssetDetail&k=1&v=1&catId=297459&assetId=1073456";

    @Override
    protected void doCreate(Bundle savedInstanceState) {
        WLog.i(TAG,"doCreate()");

        setContentView(R.layout.activity_column_detail);

        JSON_URL = getIntent().getStringExtra(IntentConstant.DATAURI.value());

        context = this;

        if(TextUtils.isEmpty(JSON_URL)) {
            showErrorExitDlg("没有数据源!");
            return;
        }

        mTitleView = (TextView) findViewById(R.id.titleView);
        mListView = (ChannelListView) findViewById(R.id.listView);
        mBgImageView = (ImageView)findViewById(R.id.bgImage);

        initListView();

        fetchAssetInfo();
    }

    private void initListView() {

        mAdapter = new ColumnDetailAdapter();
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                view.performClick();
                mAssetDetailData.setCurPlayIndex(i);
                mAssetDetailData.setDetailUrl(JSON_URL);

                Intent intent=new Intent(context,ActivityPlayer.class);
                intent.putExtra(ActivityPlayer.ASSET_INFO, mAssetDetailData);
                startActivity(intent);
            }
        });
        mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mListView.setFocusItem(view);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    private void updateUI() {

        if(mItemList != null && mItemList.size() > 0) {
            mAdapter.setData(mItemList);
            mAdapter.notifyDataSetChanged();
        }

        if (mAssetDetailData != null) {
            mTitleView.setText(mAssetDetailData.getCatName());
            FrescoImageFetcherModule.getInstance().attachImage(mAssetDetailData.getBgPicUrl(),mBgImageView);
        }
    }

    public void fetchAssetInfo() {

        DataFetchListener.ObjectListener objectListener = new DataFetchListener.ObjectListener() {

            @Override
            public void onObjectGet(int retcode, String extraMsg, ObjectBase data) {

                if (retcode != 0) {
                    showErrorExitDlg("获取数据失败");
                    return;
                } else {
                    mAssetDetailData = (DemandProgram) data;
                    mItemList = mAssetDetailData.getFirstPlayinfo().getSeriesList();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            updateUI();
                        }
                    });
                }

            }
        };

        DataFetchModule.getInstance().fetchObjectGet(JSON_URL, DemandProgram.class, objectListener);
    }

    class ColumnDetailAdapter extends BaseAdapter{

        private List<Serializable> mData = new ArrayList<Serializable>();

        public void setData(List<? extends Serializable> data) {
            this.mData.clear();
            this.mData.addAll(data);
        }


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

        @Override
        public Serializable getItem(int i) {
            Serializable res = null;
            if(i < mData.size()) {
                res = mData.get(i);
            }
            return res;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ColumnDetailListItem item;
            if(convertView == null) {
                item = new ColumnDetailListItem(ActivityColumnDetail.this);
            }
            else {
                item = (ColumnDetailListItem)convertView;
            }

            item.setData((DemandSeries) mAdapter.getItem(position));

            return item;
        }
    }
}
