package com.ycgis.macall.personalcenter.v.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ItemApplySelectRoleBinding;
import com.ycgis.macall.personalcenter.databinding.ItemSelecctRoleBinding;
import com.ycgis.macall.personalcenter.databinding.ItemSelectAppBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.ApplyAppBean;
import com.ycgis.macall.personalcenter.m.adapterbean.BaseBean;
import com.ycgis.macall.personalcenter.m.adapterbean.SelectAppBean;
import com.ycgis.macall.personalcenter.m.adapterbean.SelectRoleBean;
import com.ycgis.macall.personalcenter.p.app.AppCache;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2023/6/1 16:25
 * copyright:    @ruansee.com
 * Describe:     选择系统和角色
 */
public class SelectSystemAdapter extends BaseRecyclerAdapter<BaseBean, BaseRecyclerAdapter.ViewHolder> {

    public SelectSystemAdapter(Context context, List<BaseBean> dataList) {
        super(context, dataList);
    }

    @Override
    public int getItemViewType(int position) {
        return getDataItem(position).getBaseType();
    }

    @NonNull
    @NotNull
    @Override
    public BaseRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == 0 || viewType == -1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_error, parent, false);
            return new ErrorViewHolder(view);
        }
        if (viewType == 1) {
            View view = bindView(parent, R.layout.item_select_app);
            ItemSelectAppBinding binding = ItemSelectAppBinding.bind(view);
            return new ViewBindingHolder<>(binding);
        }
        if (viewType == 2) {
            View view = bindView(parent, R.layout.item_selecct_role);
            ItemSelecctRoleBinding binding = ItemSelecctRoleBinding.bind(view);
            return new ViewBindingHolder<>(binding);
        }
        if (viewType == 3) {
            View view = bindView(parent, R.layout.item_selecct_role);
            ItemSelecctRoleBinding binding = ItemSelecctRoleBinding.bind(view);
            return new ShowRoleViewHolder(binding);
        }
        if (viewType == 4) {
            View view = bindView(parent, R.layout.item_apply_select_role);
            ItemApplySelectRoleBinding binding = ItemApplySelectRoleBinding.bind(view);
            return new ViewBindingHolder<>(binding);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        BaseBean item = getDataItem(position);
        if (viewHolder instanceof BaseRecyclerAdapter.ViewBindingHolder) {
            ViewBindingHolder holder = (ViewBindingHolder) viewHolder;
            if (holder.viewBind instanceof ItemApplySelectRoleBinding){
                ItemApplySelectRoleBinding binding = (ItemApplySelectRoleBinding) holder.viewBind;
                if (item instanceof ApplyAppBean.DataBean2){
                    ApplyAppBean.DataBean2 dataItem = (ApplyAppBean.DataBean2) item;
                    if (dataItem.isSelect()) {
                        binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_item_sel));
                        binding.getRoot().setBackground(ContextCompat.getDrawable(context, R.color.colorGreen4));
                    } else {
                        binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.select_def));
                        binding.getRoot().setBackground(ContextCompat.getDrawable(context, R.color.colorWhite));
                    }
                    if (StringUtil.hasContent(dataItem.getRoleName())){
                        StringBuilder builder = new StringBuilder();
                        if (dataItem.getRoleName().contains("普通")){
                            builder.append("<font color=\"#323232\">");
                            builder.append(dataItem.getRoleName());
                            builder.append("</font><font color=\"#888888\"> (");
                            builder.append(dataItem.getRemark());
                            builder.append(")</font>");
                        }else {
                            builder.append("<font color=\"#fb8c00\">");
                            builder.append(dataItem.getRoleName());
                            builder.append("</font><font color=\"#ffcc80\"> (");
                            builder.append(dataItem.getRemark());
                            builder.append(")</font>");
                        }
                        binding.tvAppName.setText(Html.fromHtml(builder.toString()));
                    }

                }
                return;
            }

            if (holder.viewBind instanceof ItemSelectAppBinding) {
                ItemSelectAppBinding binding = (ItemSelectAppBinding) holder.viewBind;
                if (item instanceof SelectAppBean) {
                    SelectAppBean dataItem = (SelectAppBean) item;
                    binding.tvAppName.setText(dataItem.getAppName());
                    if (dataItem.getIcon() == null) {
                        setImg(binding.ivAppIcon, dataItem.getIconUrl());
                    } else {
                        //图片加载自己实现
                        Glide.with(context)
                                .load(dataItem.getIcon())
                                .placeholder(R.drawable.icon_gd)
                                .error(R.drawable.icon_error2)
                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                                .into(binding.ivAppIcon);
                    }
                    if (dataItem.isSelect()) {
                        binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_select));
                        binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.blue));
                    } else {
                        binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_not_select));
                        binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorGrey8));
                    }
                }
                if (item instanceof ApplyAppBean.DataBean) {
                    ApplyAppBean.DataBean dataItem = (ApplyAppBean.DataBean) item;
                    binding.tvAppName.setText(dataItem.getName());
                    if (dataItem.getIcon() != null) {
                        Glide.with(context)
                                .load(dataItem.getIconUrl())
                                .placeholder(R.drawable.icon_gd)
                                .error(R.drawable.icon_error2)
                                .into(binding.ivAppIcon);
                    }
                    if (dataItem.isSelect()) {
                        binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_select));
                        binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.blue));
                        binding.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_bottom));
                    } else {
                        binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_not_select));
                        binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorGrey8));
                        binding.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_right));
                    }
                }
                return;
            }
            if (holder.viewBind instanceof ItemSelecctRoleBinding) {
                ItemSelecctRoleBinding binding = (ItemSelecctRoleBinding) holder.viewBind;
                SelectRoleBean dataItem = (SelectRoleBean) getDataItem(position);
                binding.tvAppName.setText(dataItem.getName());
                if (dataItem.isSelect()) {
                    binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_item_sel));
                } else {
                    binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.select_def));
                }
                return;
            }
        }
        if (viewHolder instanceof ShowRoleViewHolder) {
            ShowRoleViewHolder holder = (ShowRoleViewHolder) viewHolder;
            SelectRoleBean dataItem = (SelectRoleBean) getDataItem(position);
            holder.binding.tvAppName.setText(dataItem.getName());
            holder.binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_role_sel));
            holder.binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorBluen7));
        }
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder) {
            ErrorViewHolder holder = (ErrorViewHolder) viewHolder;
            if (getDataItem(position).getBaseType() == 0) {
                holder.setEmpty(getDataItem(position).getBaseMessage());
            } else {
                holder.setError(getDataItem(position).getBaseMessage());
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position, @NonNull @NotNull List<Object> payloads) {
        if (!payloads.isEmpty()) {
            Object o = payloads.get(0);
            if (!(o instanceof Integer)) return;
            int a = (int) o;
            if (a == 1) {
                BaseBean item = getDataItem(position);
                if (viewHolder instanceof BaseRecyclerAdapter.ViewBindingHolder) {
                    ViewBindingHolder holder = (ViewBindingHolder) viewHolder;
                     if (holder.viewBind instanceof ItemApplySelectRoleBinding) {
                         ItemApplySelectRoleBinding binding = (ItemApplySelectRoleBinding) holder.viewBind;
                         if (item instanceof ApplyAppBean.DataBean2) {
                             ApplyAppBean.DataBean2 dataItem = (ApplyAppBean.DataBean2) item;
                             if (dataItem.isSelect()) {
                                 binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_item_sel));
                                 binding.getRoot().setBackground(ContextCompat.getDrawable(context, R.color.colorGreen4));
                             } else {
                                 binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.select_def));
                                 binding.getRoot().setBackground(ContextCompat.getDrawable(context, R.color.colorWhite));
                             }
                         }
                        return;
                     }
                    if (holder.viewBind instanceof ItemSelectAppBinding) {
                        ItemSelectAppBinding binding = (ItemSelectAppBinding) holder.viewBind;
                        if (item instanceof ApplyAppBean.DataBean) {
                            ApplyAppBean.DataBean dataItem = (ApplyAppBean.DataBean) item;
                            if (dataItem.isSelect()) {
                                binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_select));
                                binding.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_bottom));
                                binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.blue));
                            } else {
                                binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_not_select));
                                binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorGrey8));
                                binding.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_right));
                            }
                        }

                        if (item instanceof SelectAppBean) {
                            SelectAppBean dataItem = (SelectAppBean) item;
                            if (dataItem.isSelect()) {
                                binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_select));
                                binding.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_bottom));
                                binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.blue));
                            } else {
                                binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_not_select));
                                binding.tvAppName.setTextColor(ContextCompat.getColor(context, R.color.colorGrey8));
                                binding.imageView16.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_arrow_right));
                            }
                        }
                        return;
                    }
                    if (holder.viewBind instanceof ItemSelecctRoleBinding) {
                        ItemSelecctRoleBinding binding = (ItemSelecctRoleBinding) holder.viewBind;
                        SelectRoleBean dataItem = (SelectRoleBean) getDataItem(position);
                        binding.tvAppName.setText(dataItem.getName());
                        if (dataItem.isSelect()) {
                            binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_item_sel));
                        } else {
                            binding.ivSelect.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.select_def));
                        }
                    }
                }
                return;
            }
            if (a == 6) {
                if (viewHolder instanceof ShowRoleViewHolder) {
                    ShowRoleViewHolder holder = (ShowRoleViewHolder) viewHolder;
                    SelectRoleBean dataItem = (SelectRoleBean) getDataItem(position);
                    holder.binding.tvAppName.setText(dataItem.getName());
                }
            }
        }

        super.onBindViewHolder(viewHolder, position, payloads);
    }

    private void setImg(ImageView img, String url) {
        String imgUrl = AppCache.BASE_IMAGE_URL + url;
//        String imgUrl = "http://" + RuanseeApplication.getAppCache().getIp() + ":8080" + url;
        //图片加载自己实现
        Glide.with(img)
                .load(imgUrl)
                .placeholder(R.drawable.icon_gd)
                .error(R.drawable.icon_error2)
                .into(img);
    }

    public class ShowRoleViewHolder extends ViewHolder {
        ItemSelecctRoleBinding binding;

        public ShowRoleViewHolder(ItemSelecctRoleBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
