package com.kunminx.linkage.defaults;
/*
 * Copyright (c) 2018-present. KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.kunminx.linkage.R;
import com.kunminx.linkage.adapter.viewholder.LinkagePrimaryViewHolder;
import com.kunminx.linkage.contract.ILinkagePrimaryAdapterConfig;

/**
 * Create by KunMinX at 19/5/8
 */
public class DefaultLinkagePrimaryAdapterConfig implements ILinkagePrimaryAdapterConfig {

  private static final int MARQUEE_REPEAT_LOOP_MODE = -1;
  private static final int MARQUEE_REPEAT_NONE_MODE = 0;
  private Context mContext;
  private OnPrimaryItemBindListener mListener;
  private OnPrimaryItemClickListener mClickListner;

  public void setListener(OnPrimaryItemBindListener listener,
                          OnPrimaryItemClickListener clickListener) {
    mListener = listener;
    mClickListner = clickListener;
  }

  @Override
  public void setContext(Context context) {
    mContext = context;
  }

  @Override
  public int getLayoutId() {
    return R.layout.default_adapter_linkage_primary;
  }

  @Override
  public int getGroupTitleViewId() {
    return R.id.tv_group;
  }

  @Override
  public int getRootViewId() {
    return R.id.layout_group;
  }

  @Override
  public void onBindViewHolder(LinkagePrimaryViewHolder holder, boolean selected, String title) {
    TextView tvTitle = ((TextView) holder.getGroupTitle());
    tvTitle.setText(title);

    tvTitle.setBackgroundColor(mContext.getResources().getColor(selected ? R.color.colorPurple : R.color.colorWhite));
    tvTitle.setTextColor(ContextCompat.getColor(mContext, selected ? R.color.colorWhite : R.color.colorGray));
    tvTitle.setEllipsize(selected ? TextUtils.TruncateAt.MARQUEE : TextUtils.TruncateAt.END);
    tvTitle.setFocusable(selected);
    tvTitle.setFocusableInTouchMode(selected);
    tvTitle.setMarqueeRepeatLimit(selected ? MARQUEE_REPEAT_LOOP_MODE : MARQUEE_REPEAT_NONE_MODE);

    if (mListener != null) {
      mListener.onBindViewHolder(holder, title);
    }
  }

  @Override
  public void onItemClick(LinkagePrimaryViewHolder holder, View view, String title) {
    if (mClickListner != null) {
      mClickListner.onItemClick(holder, view, title);
    }
  }

  public interface OnPrimaryItemClickListener {
    /**
     * we suggest you get position by holder.getBindingAdapterPosition
     *
     * @param holder primaryHolder
     * @param view   view
     * @param title  groupTitle
     */
    void onItemClick(LinkagePrimaryViewHolder holder, View view, String title);
  }

  public interface OnPrimaryItemBindListener {
    /**
     * Note: Please do not override rootView click listener in here, because of linkage selection rely on it.
     * and we suggest you get position by holder.getBindingAdapterPosition
     *
     * @param primaryHolder primaryHolder
     * @param title         groupTitle
     */
    void onBindViewHolder(LinkagePrimaryViewHolder primaryHolder, String title);
  }
}
