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

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
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.ToastUtils;

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

import cn.muchinfo.rma.R;
import cn.muchinfo.rma.global.data.ExposureRealTimeDetailsData;
import cn.muchinfo.rma.global.data.RealExposureData;
import cn.muchinfo.rma.view.autoWidget.CustomHorizontalScrollView;
import cn.muchinfo.rma.view.base.procurement.RightScrollAdapter;

/**
 * 用于敞口的期货详情和现货详情的列表adapter
 */
public class ExposureDetailsContentAdapter extends RecyclerView.Adapter<ExposureDetailsContentAdapter.ItemViewHolder> {

    private Context context;
    private List<ExposureRealTimeDetailsData> datas;
    private int index = -1;
    private int position = -1;
    private List<ItemViewHolder> mViewHolderList = new ArrayList<>();
    public int offestX = 0;
    private OnContentScrollListener onContentScrollListener;
    private String type;//1是现货 2是期货

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

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


    public ExposureDetailsContentAdapter(Context context,String type) {
        this.type = type;
        this.context = context;
    }

    public void setDatas(List<ExposureRealTimeDetailsData> 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_exposure_content, viewGroup, false);
        return new ItemViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolder itemViewHolder, int i) {
        this.index = i;
        if ("1".equals(type)){//现货头部左侧
            itemViewHolder.exposure_tvLeftTitle.setText(datas.get(i).getCreatetime());
            itemViewHolder.exposure_tv_left_bottom_title.setText(getType(datas.get(i).getContracttype(),datas.get(i).getLogtype()));
        }else if ("2".equals(type)){//期货头部左侧
            itemViewHolder.exposure_tvLeftTitle.setText(datas.get(i).getGoodsname());
            itemViewHolder.exposure_tv_left_bottom_title.setText(datas.get(i).getGoodscode());
        }

        //右边滑动部分
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        itemViewHolder.exposure_rvItemRight.setLayoutManager(linearLayoutManager);
        itemViewHolder.exposure_rvItemRight.setHasFixedSize(true);
        RightScrollAdapter rightScrollAdapter = new RightScrollAdapter(context);
        rightScrollAdapter.setDatas(datas.get(i).getRightData());
        itemViewHolder.exposure_rvItemRight.setAdapter(rightScrollAdapter);
        //缓存当前holder
        if (!mViewHolderList.contains(itemViewHolder)) {
            mViewHolderList.add(itemViewHolder);
        }

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

    private String getType(String contracttype,String logtype){
        String contracttypeString;
        String logtypeString;
        if (contracttype.equals("1")){
            contracttypeString = "采购";
        }else {
            contracttypeString = "销售";
        }

        if (logtype.equals("1")){
            logtypeString = "计划";
        }else {
            logtypeString = "合同";
        }
        return contracttypeString + logtypeString;
    }

    @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 exposure_roots_view;//根部布局
        TextView spot_subsidiary;//现货明细
        TextView futures_subsidiary;//期货明细
        TextView place_the_order;//下单
        LinearLayout exposure_allView;//隐藏的底部操作空间
        LinearLayout exposure_all_click_View;//item左侧点击控件
        TextView exposure_tvLeftTitle;//左边
        TextView exposure_tv_left_bottom_title;//合同编号
        RecyclerView exposure_rvItemRight;//右方的列表
        public CustomHorizontalScrollView exposure_horItemScrollview;//右方的滑动控件
        private boolean isLayoutFinish;//自定义字段,用于标记layout

        public boolean isLayoutFinish() {
            return isLayoutFinish;
        }

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

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            spot_subsidiary = itemView.findViewById(R.id.spot_subsidiary);
            futures_subsidiary = itemView.findViewById(R.id.futures_subsidiary);
            place_the_order = itemView.findViewById(R.id.place_the_order);
            exposure_tvLeftTitle = itemView.findViewById(R.id.exposure_tv_left_title);
            exposure_rvItemRight = itemView.findViewById(R.id.exposure_rv_item_right);
            exposure_horItemScrollview = itemView.findViewById(R.id.exposure_hor_item_scrollview);
            exposure_allView = itemView.findViewById(R.id.exposure_all_view);
            exposure_tv_left_bottom_title = itemView.findViewById(R.id.exposure_tv_left_bottom_title);
            exposure_all_click_View = itemView.findViewById(R.id.exposure_all_click_View);
            exposure_roots_view = itemView.findViewById(R.id.exposure_roots_view);
        }
    }
}
