package com.uas.module.girls;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uas.module.common.base.fragment.EasyFragment;
import com.uas.module.common.model.MessageConfigBean;
import com.uas.module.common.utils.Constants;
import com.uas.module.common.widget.LinearItemDecoration;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;


public class MessageFragment extends EasyFragment {

    RecyclerView mListMessage;
    
    public static MessageFragment newInstance() {
        return new MessageFragment();
    }

    
    @Override
    protected int inflateLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void onCreateView(Bundle savedInstanceState, boolean createView) {
          if (createView){
              initView();
          }
    }

    
    public void initView(){
        mListMessage=  findViewById(R.id.rv_message_list);
        mListMessage.setLayoutManager(new LinearLayoutManager(ct));
        mListMessage.addItemDecoration(new LinearItemDecoration(ct));
        String messageList= SPUtils.getInstance(Constants.spName).getString("messageList");
        List<MessageConfigBean> datas= JSON.parseArray(messageList,MessageConfigBean.class);
      
        mListMessage.setAdapter(new RecyclerAdapter(datas));


        RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
    }


    class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> {
        private List<MessageConfigBean> data;

        public RecyclerAdapter(List<MessageConfigBean> datas) {
            data = datas;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(getActivity())
                    .inflate(R.layout.item_message, parent, false));
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            MessageConfigBean bean=data.get(position);
            holder.titleTv.setText(bean.getName());
            holder.bean=bean;
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            Badge badge;
            CircleImageView headerImg;
            TextView  titleTv;
            TextView  headerSubTv;
            MessageConfigBean bean;
            
            public Holder(View itemView) {
                super(itemView);
                headerImg=itemView.findViewById(R.id.headerImg);
                titleTv= itemView.findViewById(R.id.titleTv);
                headerSubTv= itemView.findViewById(R.id.headerSubTv);
                badge = new QBadgeView(getActivity()).bindTarget(itemView.findViewById(R.id.headerImg));
                badge.setBadgeGravity(Gravity.TOP | Gravity.END);
                badge.setBadgeTextSize(9, true);
                badge.setBadgePadding(2, true);
                badge.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                    @Override
                    public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                        if (dragState == STATE_SUCCEED) {
                            Toast.makeText(getActivity(), String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
                        }
                    }
                }).setBadgeNumber(1);
            }
        }
    }


}
