package kr.co.hanwha.hifive.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectedMemberPagerContract;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.util.GlideUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * Add TASK : 선택된 멤버페이지뷰 어댑터
 *
 * @see kr.co.hanwha.hifive.screen.task.AddTaskSelectMemberActivity
 * Created by sanghoonkwon on 2018. 1. 18..
 */

public class AddTaskSelectedMemberPagerAdapter extends PagerAdapter
        implements AddTaskSelectedMemberPagerContract.Model, AddTaskSelectedMemberPagerContract.View {
    private final int ZERO = 0;

    private LayoutInflater mLayoutInflater;
    private AddTaskSelectedMemberPagerContract.OnMemberPagerListener mOnMemberPagerListener;
    private ArrayList<MemberInfoData> mListData;
    private Context mContext;

    private boolean isSecondStep = false;

    public AddTaskSelectedMemberPagerAdapter(@NonNull Context context, AddTaskSelectedMemberPagerContract.OnMemberPagerListener onMemberPagerListener) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        mOnMemberPagerListener = onMemberPagerListener;
    }

    public AddTaskSelectedMemberPagerAdapter(@NonNull Context context, boolean isSecondStep, AddTaskSelectedMemberPagerContract.OnMemberPagerListener onMemberPagerListener) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        this.isSecondStep = isSecondStep;
        mOnMemberPagerListener = onMemberPagerListener;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = this.mLayoutInflater.inflate(R.layout.item_selected_member_pager, container, false);

        ImageView imageViewProfile = (ImageView) view.findViewById(R.id.profile_imageView);
        TextView textViewName = (TextView) view.findViewById(R.id.name_textView);
        ImageButton imageButtonDelete = (ImageButton) view.findViewById(R.id.delete_imageButton);


        final int index = position;
        final MemberInfoData data = mListData.get(index);

        if (TextUtils.isEmpty(data.getProfile_url()))
            GlideUtil.getInstance().loadImageCircle(getContext(), R.mipmap.ic_avatar, imageViewProfile);
        else
            GlideUtil.getInstance().loadImageCircle(getContext(), data.getProfile_url(), imageViewProfile);

        textViewName.setText(data.getMember_name());

        if (isSecondStep) imageButtonDelete.setVisibility(View.GONE);
        else imageButtonDelete.setVisibility(View.VISIBLE);

        imageButtonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnMemberPagerListener != null)
                    mOnMemberPagerListener.onDeleteClick(data, index);
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public float getPageWidth(int position) {
        float pageWidth = (float) HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.add_task_member_pager_width) / (float) HFCommonUtil.getScreenWidth(getContext());
        return pageWidth;
    }


    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return (mListData != null ? mListData.size() : ZERO);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void notifyDataAll() {
        notifyDataSetChanged();
    }

    @Override
    public void addItems(@NonNull ArrayList<MemberInfoData> list) {
        mListData = list;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**************************************************
     * Support methods
     **************************************************/
    private Context getContext() {
        return mContext;
    }
}