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.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

public class HFReceiverContainerView extends LinearLayout {
    private LinearLayout mLayoutContainer;
    private TextView mTextViewWriteToMe;

    private OnReceiverContainerListener mOnReceiverContainerListener;

    private ArrayList<MemberInfoData> mListOfMembers;

    public void setOnReceiverContainerListener(OnReceiverContainerListener onReceiverContainerListener) {
        this.mOnReceiverContainerListener = onReceiverContainerListener;
    }

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

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

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

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        if (isInEditMode()) return;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_receiver_container, this, true);

        mLayoutContainer = (LinearLayout) findViewById(R.id.container_linearLayout);

        mTextViewWriteToMe = (TextView) findViewById(R.id.writeToMe_textView);

        mTextViewWriteToMe.setOnClickListener(onClickListener);
        findViewById(R.id.selectMember_imageView).setOnClickListener(onClickListener);
    }

    /**
     * 받는 사람 목록 표시
     *
     * @param listOfMembers
     */
    public void setMemberList(@NonNull ArrayList<MemberInfoData> listOfMembers) {
        mListOfMembers = listOfMembers;

        if (mListOfMembers == null || mListOfMembers.size() == 0) {
            findViewById(R.id.container_linearLayout).setVisibility(View.GONE);
            findViewById(R.id.hint_textView).setVisibility(View.VISIBLE);
            mTextViewWriteToMe.setSelected(false);
            mTextViewWriteToMe.setVisibility(View.VISIBLE);
            return;
        } else {
            if (mListOfMembers.size() == 1 && mListOfMembers.get(0).getMember_id() == MemberDataRepository.getInstance().getMemberId()) {
                mTextViewWriteToMe.setSelected(true);
                mTextViewWriteToMe.setVisibility(View.VISIBLE);
            } else {
                mTextViewWriteToMe.setSelected(false);
                mTextViewWriteToMe.setVisibility(View.GONE);
            }
        }

        findViewById(R.id.hint_textView).setVisibility(View.GONE);
        findViewById(R.id.container_linearLayout).setVisibility(View.VISIBLE);

        int screenWidth = HFCommonUtil.getScreenWidth(getContext());
        int margin = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp100);
        int containerWidth = screenWidth - margin;
        mLayoutContainer.removeAllViews();

        int count = mListOfMembers.size();
        for (int i = 0; i < mListOfMembers.size(); i++) {
            HFReceiverView view = new HFReceiverView(getContext());
            view.setMemberInfo(mListOfMembers.get(i));
            view.measure(0, 0);
            int memberWidth = view.getMeasuredWidth();

            HFReceiverEtcView etcView = new HFReceiverEtcView(getContext());
            etcView.setCount(count);
            etcView.measure(0, 0);
            int etcWidth = etcView.getMeasuredWidth();

            if (containerWidth - memberWidth > 0) {
                containerWidth -= memberWidth;
                count -= 1;
                mLayoutContainer.addView(view);

                if (containerWidth - etcWidth < 0) {
                    mLayoutContainer.addView(etcView);
                } else {
                    // TODO !!!
                }
            } else {
                if (containerWidth - etcWidth > 0) {
                    containerWidth -= etcWidth;
                    etcView.setCount(count);
                    mLayoutContainer.addView(etcView);
                } else {
                    // TODO !!!
                }
            }
        }
    }

    public void setEdtiable(@Nullable boolean isEditable) {
        if (!isEditable) {
            mTextViewWriteToMe.setVisibility(View.GONE);
            mTextViewWriteToMe.setOnClickListener(null);
            findViewById(R.id.selectMember_imageView).setVisibility(View.GONE);
            findViewById(R.id.selectMember_imageView).setOnClickListener(null);
        }
    }

    /**
     * 내게 쓰기
     */
    private void writeToMe() {
        if (mListOfMembers != null &&
                mListOfMembers.size() == 1 &&
                mListOfMembers.get(0).getMember_id() == MemberDataRepository.getInstance().getMemberId()) {
            TaskDataRepository.setMemeberListSelectMine(false);
        } else {
            TaskDataRepository.setMemeberListSelectMine(true);
        }

        setMemberList(TaskDataRepository.getSelectedDataList());

        if (mOnReceiverContainerListener != null)
            mOnReceiverContainerListener.onWriteToMeClicked();
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.selectMember_imageView:
                    if (mOnReceiverContainerListener != null)
                        mOnReceiverContainerListener.onAddMemberClicked();
                    break;
                case R.id.writeToMe_textView:
                    writeToMe();
                    break;
            }

        }
    };

    /**************************************************
     * OnReceiverContainerListener
     **************************************************/
    public interface OnReceiverContainerListener {
        void onAddMemberClicked();

        void onWriteToMeClicked();
    }
}