package com.wasu.cs.ui;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wasu.cs.model.CatData;
import com.wasu.cs.model.CatData.AssetElement;
import com.wasu.cs.protocol.CatProtocol;
import com.wasu.cs.widget.FocusLinearLayoutEx;
import com.wasu.cs.widget.SpecialTpItem;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.module.log.WLog;

import java.util.List;

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

/**
 * 活动专题页面
 * @author LiuYang
 * Created by LiuYang on 7/15/15.
 */
public class ActivityActSpecial extends ActivityBase {
    private String TAG = "ActivityActSpecial";

    private ImageView mBgImage;

    //标题
    private TextView mTopText;
    private TextView mBottomText;

    //头像
    private ImageView mTopImage;
    private ImageView mBottomImage;

    //内容
    private FocusLinearLayoutEx mTopLayout;
    private LinearLayout mBottomLayout;

    private int mCurrentIndex = 0;
    private List<AssetElement> mAssets;

    private Handler mHandler = new Handler();
    private String CAT_URL;// = "http://121.40.195.74/?s=2002&p=sntAssetList&k=1&v=1&catId=276046&eid=155&ecatId=276100";

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

        setContentView(R.layout.activity_act_special);

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

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

        initView();
        requestData();
    }

    private void initView() {

        mBgImage = (ImageView) findViewById(R.id.bgImage);

        mTopText = (TextView) findViewById(R.id.topText);
        mBottomText = (TextView) findViewById(R.id.bottomText);

        mTopImage = (ImageView) findViewById(R.id.topImage);
        mBottomImage = (ImageView) findViewById(R.id.bottomImage);

        mTopLayout = (FocusLinearLayoutEx) findViewById(R.id.topLayout);
        mTopLayout.setFocusHighlightDrawable(R.drawable.tv_select_focus);
        mTopLayout.setFocusRealId(R.id.ivPic);
        mTopLayout.setFocusMovingDuration(200);

        mBottomLayout = (LinearLayout) findViewById(R.id.bottomLayout);
    }

    private void updateUI(final CatData catData) {
        FrescoImageFetcherModule.getInstance().attachImage(catData.getCat().getBgImage(),mBgImage);
        mTopLayout.requestFocus();
        mAssets = catData.getAssets();
        if (mAssets==null||mAssets.size()<=0) {
			return;
		}
        updateLayout();
    }

    private void updateLayout() {
        if (mAssets.size()>mCurrentIndex) {
        AssetElement topAsset = mAssets.get(mCurrentIndex);
        mTopText.setText(topAsset.getCatName());
        FrescoImageFetcherModule.getInstance().attachImage(topAsset.getBgImage(), mTopImage);
        updateLayout(mTopLayout, topAsset, true);
		
		}
        int bottomIndex = mCurrentIndex + 1;
        if(bottomIndex == mAssets.size()) {
            bottomIndex = 0;
        }
        if (mAssets.size()>bottomIndex) {
        AssetElement bottomAsset = mAssets.get(bottomIndex);
        mBottomText.setText(bottomAsset.getCatName());
        FrescoImageFetcherModule.getInstance().attachImage(bottomAsset.getBgImage(), mBottomImage);
        updateLayout(mBottomLayout, bottomAsset, false);
		}
    }

    private void updateLayout(LinearLayout layout,AssetElement assetElement,boolean focus) {
        if (assetElement==null||assetElement.getDetails()==null) {
			return;
		}
        for(int i = 0;i < assetElement.getDetails().size();i++) {
            boolean needAdd = false;
            SpecialTpItem item = null;
            if(i < layout.getChildCount()) {
                item = (SpecialTpItem)layout.getChildAt(i);
            }
            else {

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
                int marginValue = getResources().getDimensionPixelSize(R.dimen.d_15dp);
                params.leftMargin = marginValue;
                params.rightMargin = marginValue;
                params.weight = 1;
                item = new SpecialTpItem(this);
                item.setLayoutParams(params);
                item.setFocusEnable(focus);
                needAdd = true;
            }

            item.setData(assetElement.getDetails().get(i));
            if(needAdd) {
                layout.addView(item);
            }
        }
    }

    //请求数据
    private void requestData() {
        CatProtocol.fetchData(mHandler, CAT_URL, new CatProtocol.CatFetchCallback() {
            @Override
            public void onResult(boolean successed, CatData catData) {
                if (successed) {
                    updateUI(catData);
                } else {
                    showErrorExitDlg("获取数据失败");
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if(mCurrentIndex < mAssets.size() - 1) {
                mCurrentIndex++;
            }
            else {
                mCurrentIndex = 0;
            }
            updateLayout();
        }
        else if(keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if(mCurrentIndex == 0) {
                mCurrentIndex = mAssets.size() - 1;
            }
            else {
                mCurrentIndex--;
            }

            updateLayout();
        }
        return super.onKeyDown(keyCode, event);
    }
}
