package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

/**
 * Created by sanghoonkwon on 2018. 1. 23..
 */

public class HFSearchRecentView extends LinearLayout {
    private LinearLayout mLayoutRecentContainer;

    private OnSearchRecentListener onSearchRecentListener;

    public void setOnSearchRecentListener(OnSearchRecentListener onSearchRecentListener) {
        this.onSearchRecentListener = onSearchRecentListener;
    }

    public HFSearchRecentView(Context context) {
        super(context);
        initialize(context, null);
    }

    public HFSearchRecentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public HFSearchRecentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_search_recent, this, true);

        mLayoutRecentContainer = (LinearLayout) findViewById(R.id.recentContainer_linearLayout);
    }

    public void setRecentKeyword(ArrayList<String> list) {
        int screenWidth = HFCommonUtil.getScreenWidth(getContext());
        int dp16 = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp16);

        int lastLine = 0;
        int containerWidth = screenWidth - dp16 * 2;
        mLayoutRecentContainer.removeAllViews();
        mLayoutRecentContainer.addView(getHorizontalLayout(Integer.toString(lastLine)));


        for (int i = 0; i < list.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_search_recent_keyword, mLayoutRecentContainer, false);
            TextView textViewTitleTag = view.findViewById(R.id.title_textView);
            ImageView imageViewClear = view.findViewById(R.id.clear_imageView);

            view.setTag(list.get(i));
            imageViewClear.setTag(list.get(i));
            textViewTitleTag.setText(list.get(i));

            imageViewClear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    HFInfoUtil.deleteRecentKeyword(getContext(), (String) view.getTag());
                    if (onSearchRecentListener != null)
                        onSearchRecentListener.keywordDeleted();
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String keyword = (String) view.getTag();
                    if (onSearchRecentListener != null)
                        onSearchRecentListener.keywordClicked(keyword);
                }
            });

            view.measure(0, 0);
            int measuredWidth = view.getMeasuredWidth();
            if (containerWidth - measuredWidth < 0) {
                lastLine += 1;
                containerWidth = screenWidth - dp16 * 2;
                mLayoutRecentContainer.addView(getHorizontalLayout(Integer.toString(lastLine)));
            }
            containerWidth -= measuredWidth;

            ((LinearLayout) mLayoutRecentContainer.findViewWithTag(Integer.toString(lastLine))).addView(view);
        }
    }

    private LinearLayout getHorizontalLayout(String tag) {
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setTag(tag);
        return linearLayout;
    }

    /**************************************************
     * OnSearchRecentListener
     **************************************************/
    public interface OnSearchRecentListener {
        void keywordClicked(@NonNull String keyword);

        void keywordDeleted();
    }
}
