package com.wasu.cs.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.wasu.main.R;

import com.wasu.cs.model.SearchModel.Assets;
import com.wasu.cs.ui.FragmentSearchResult.CatalogChangeListener;

public class SearchResultCatalogItem extends LinearLayout {

    public static int ID = 0x304;
    private int mIndex;
    private static int selectIndex;

    private TextView mCatalogTV;
    private View mDividingV;

    private Assets mAssets;

    private CatalogChangeListener mListener;

    public Assets returnAssets() {
        return mAssets;
    }

    public SearchResultCatalogItem(Context context) {
        super(context);
        initLayout(context);
    }

    public SearchResultCatalogItem(Context context, AttributeSet attrs,
                                   int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout(context);
    }

    public SearchResultCatalogItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout(context);
    }

    private void initLayout(Context context) {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setFocusable(true);
        setFocusableInTouchMode(true);

        setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    selectIndex = mIndex;
                    if (null != mListener) {
                        mListener.onCatalogChange(SearchResultCatalogItem.this);
                    }
                    mCatalogTV.setTextColor(Color.YELLOW);
                    mDividingV.setVisibility(GONE);
                } else {
                    mCatalogTV.setTextColor(Color.WHITE);
                    if (mIndex == selectIndex) {
                        mDividingV.setVisibility(VISIBLE);
                    } else {
                        mDividingV.setVisibility(GONE);
                    }
                }
            }
        });

        LayoutInflater.from(context).inflate(R.layout.item_search_result_catalog, this, true);

        mCatalogTV = (TextView) findViewById(R.id.item_search_result_catalog_tv);
        mDividingV = findViewById(R.id.item_search_result_dividing_v);
    }

    public void setData(Assets assets) {
        if (null == assets) {
            return;
        }
        mAssets = assets;

        setName();
    }

    public Assets getData() {
        return mAssets;
    }

    private void setName() {
        mCatalogTV.setText(mAssets.getCatName() + " ( " + (mAssets.getTotal() > 999 ? "999+" : mAssets.getTotal()) + " ) ");
    }

    public void onCatalogSelecte(boolean selected) {
        if (selected) {
            mDividingV.setVisibility(VISIBLE);
        } else {
            mDividingV.setVisibility(INVISIBLE);
        }
        mDividingV.setSelected(selected);
    }

    public void setCatalogChangeListener(CatalogChangeListener listener) {
        mListener = listener;
    }

    public void setIndex(int index) {
        mIndex = index;
    }

    public int getIndex() {
        return mIndex;
    }
}
