/*
 * Tencent is pleased to support the open source community by making QMUI_Android available.
 *
 * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the MIT License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/MIT
 *
 * 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.
 */

package com.gingersoft.supply_chain.mvp.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.recyclerview.widget.RecyclerView;

import com.gingersoft.gsa.cloud.ui.recylcler.decorator.MarginDecoration;
import com.gingersoft.supply_chain.R;
import com.jess.arms.utils.DeviceUtils;

/**
 * RecyclerView Adapter。
 * @author admin
 */
public class PurchasePageAdapter extends RecyclerView.Adapter<PurchasePageAdapter.ViewHolder> {

    private RecyclerView.Adapter adapter;
    private int pageSize;
    private AdapterView.OnItemClickListener mOnItemClickListener;

    public PurchasePageAdapter(RecyclerView.Adapter adapter, int pageSize) {
        this.adapter = adapter;
        this.pageSize = pageSize;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View root = inflater.inflate(R.layout.recycler_view_item, viewGroup, false);
        return new ViewHolder(root, this);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.recyclerView.setAdapter(adapter);
    }

    @Override
    public int getItemCount() {
        return pageSize;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    private void onItemHolderClick(RecyclerView.ViewHolder itemHolder) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(null, itemHolder.itemView,
                    itemHolder.getAdapterPosition(), itemHolder.getItemId());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private RecyclerView recyclerView;
        private PurchasePageAdapter mAdapter;

        public ViewHolder(View itemView, PurchasePageAdapter adapter) {
            super(itemView);
            itemView.setOnClickListener(this);
            mAdapter = adapter;
            recyclerView = itemView.findViewById(R.id.rv_purchase_list_content);
            recyclerView.addItemDecoration(new MarginDecoration((int) DeviceUtils.dpToPixel(itemView.getContext(), 8)));
        }

        @Override
        public void onClick(View v) {
            mAdapter.onItemHolderClick(this);
        }
    }
}
