package cn.muchinfo.rma.view.base.procurement;

import android.annotation.SuppressLint;
import android.content.Context;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.muchinfo.rma.R;
import cn.muchinfo.rma.global.data.ContractData;
import cn.muchinfo.rma.view.autoWidget.CustomHorizontalScrollView;
import cn.muchinfo.rma.view.autoWidget.ListPopWindow;
import cn.muchinfo.rma.view.base.home.registration.InvoiceRegistrationActivity;
import cn.muchinfo.rma.view.base.home.registration.MidpointsRegistrationActivity;
import cn.muchinfo.rma.view.base.home.registration.PaymentRegistrationActivity;
import cn.muchinfo.rma.view.base.home.registration.SettlementRegistrationActivity;

/**
 * 用于采购和销售的列表adapter
 */
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ItemViewHolder> {


    private Context context;
    private List<ContractData> datas;
    private String listType;
    private int index = -1;
    private int position = -1;
    private List<ItemViewHolder> mViewHolderList = new ArrayList<>();
    public int offestX = 0;
    private OnContentScrollListener onContentScrollListener;

    public interface OnContentScrollListener {
        void onScroll(MotionEvent event);
    }

    public void setOnContentScrollListener(OnContentScrollListener onContentScrollListener) {
        this.onContentScrollListener = onContentScrollListener;
    }


    public ContentAdapter(Context context, String listType) {
        this.context = context;
        this.listType = listType;
    }

    public void setDatas(List<ContractData> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    private void setPosition(int position) {
        if (this.position == position) {
            this.position = -1;
            notifyItemChanged(index);
        } else {
            this.position = position;
            notifyItemChanged(position);
        }
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item_content, viewGroup, false);
        return new ItemViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolder itemViewHolder, int i) {
        this.index = i;
        itemViewHolder.tvLeftTitle.setText(datas.get(i).getAccountname());
        itemViewHolder.tv_left_bottom_title.setText(datas.get(i).getContractno());
        //右边滑动部分
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        itemViewHolder.rvItemRight.setLayoutManager(linearLayoutManager);
        itemViewHolder.rvItemRight.setHasFixedSize(true);
        RightScrollAdapter rightScrollAdapter = new RightScrollAdapter(context);
        rightScrollAdapter.setDatas(datas.get(i).getRightData());
        itemViewHolder.rvItemRight.setAdapter(rightScrollAdapter);
        //缓存当前holder
        if (!mViewHolderList.contains(itemViewHolder)) {
            mViewHolderList.add(itemViewHolder);
        }
        if (i == this.position) {
            itemViewHolder.roots_view.setBackgroundColor(context.getResources().getColor(R.color.rma_list_select_color));
            itemViewHolder.allView.setVisibility(View.VISIBLE);
        } else {
            itemViewHolder.roots_view.setBackgroundColor(context.getResources().getColor(R.color.white));
            itemViewHolder.allView.setVisibility(View.GONE);
        }
        if (listType.equals("1")) {//待点价列表
            itemViewHolder.midpoints_registration.setVisibility(View.VISIBLE);
        } else if (listType.equals("2")) {//履约结算
            itemViewHolder.invoice_register.setVisibility(View.VISIBLE);
            itemViewHolder.settlement_of_registration.setVisibility(View.VISIBLE);
            itemViewHolder.payment_registration.setVisibility(View.VISIBLE);
        }
        itemViewHolder.all_click_View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPosition(i);
            }
        });
        //发票登记
        itemViewHolder.invoice_register.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("type", "1");
            intent.putExtra("data", datas.get(i));
            intent.setClass(context, InvoiceRegistrationActivity.class);
            ActivityUtils.startActivity(intent);
        });
        //点价登记
        itemViewHolder.midpoints_registration.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("type", "1");
            intent.putExtra("data", datas.get(i));
            intent.setClass(context, MidpointsRegistrationActivity.class);
            ActivityUtils.startActivity(intent);
        });
        //结算登记
        itemViewHolder.settlement_of_registration.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("type", "1");
            intent.putExtra("data", datas.get(i));
            intent.setClass(context, SettlementRegistrationActivity.class);
            ActivityUtils.startActivity(intent);
        });
        //款项登记
        itemViewHolder.payment_registration.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("type", "1");
            intent.putExtra("data", datas.get(i));
            intent.setClass(context, PaymentRegistrationActivity.class);
            ActivityUtils.startActivity(intent);
        });
        itemViewHolder.details.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("data", datas.get(i));
            intent.setClass(context, ContractDetailActivity.class);
            context.startActivity(intent);
        });
        itemViewHolder.load_more.setOnClickListener(view -> {

        });
        itemViewHolder.horItemScrollview.setEventListener(new CustomHorizontalScrollView.EventListener() {
            @Override
            public void onEvent(MotionEvent event) {
                if (null != onContentScrollListener) onContentScrollListener.onScroll(event);
            }
        });
        //由于viewHolder的缓存,在1级缓存取出来是2个viewholder,并且不会被重新赋值,所以这里需要处理缓存的viewholder的位移
        itemViewHolder.horItemScrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (!itemViewHolder.isLayoutFinish()) {
                    itemViewHolder.horItemScrollview.scrollTo(offestX, 0);
                    itemViewHolder.setLayoutFinish(true);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == datas ? 0 : datas.size();
    }

    public List<ItemViewHolder> getViewHolderCacheList() {
        return mViewHolderList;
    }

    public int getOffestX() {
        return offestX;
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        LinearLayout roots_view;//根部布局
        TextView details;//详情
        TextView invoice_register;//发票登记
        TextView midpoints_registration;//点价登记
        TextView settlement_of_registration;//结算登记
        TextView payment_registration;//款项登记
        ImageView load_more;//更多
        LinearLayout allView;//隐藏的底部操作空间
        LinearLayout all_click_View;//item左侧点击控件
        TextView tvLeftTitle;//左边销售方字段
        TextView tv_left_bottom_title;//合同编号
        RecyclerView rvItemRight;//右方的列表
        public CustomHorizontalScrollView horItemScrollview;//右方的滑动控件
        private boolean isLayoutFinish;//自定义字段,用于标记layout

        public boolean isLayoutFinish() {
            return isLayoutFinish;
        }

        public void setLayoutFinish(boolean layoutFinish) {
            isLayoutFinish = layoutFinish;
        }

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            midpoints_registration = itemView.findViewById(R.id.midpoints_registration);
            settlement_of_registration = itemView.findViewById(R.id.settlement_of_registration);
            payment_registration = itemView.findViewById(R.id.payment_registration);
            load_more = itemView.findViewById(R.id.load_more);
            tvLeftTitle = itemView.findViewById(R.id.tv_left_title);
            rvItemRight = itemView.findViewById(R.id.rv_item_right);
            horItemScrollview = itemView.findViewById(R.id.hor_item_scrollview);
            allView = itemView.findViewById(R.id.all_view);
            tv_left_bottom_title = itemView.findViewById(R.id.tv_left_bottom_title);
            all_click_View = itemView.findViewById(R.id.all_click_View);
            invoice_register = itemView.findViewById(R.id.invoice_register);
            details = itemView.findViewById(R.id.details);
            roots_view = itemView.findViewById(R.id.roots_view);
        }
    }
}
