package com.casic.dcms.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.casic.dcms.R;
import com.casic.dcms.adapter.NormalRecyclerAdapter;
import com.casic.dcms.bean.NoticeLocaleBean;
import com.casic.dcms.databinding.FragmentNoticeBinding;
import com.casic.dcms.utils.DataBaseManager;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.view.CaseDetailActivity;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.kit.ContextKit;

import java.util.List;

public class NoticePageFragment extends AndroidxBaseFragment<FragmentNoticeBinding> {

    private static final String TAG = "NoticePageFragment";
    private NormalRecyclerAdapter<NoticeLocaleBean> adapter;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        if (StringHelper.getUserBean().getId() == null) {
            getBinding().emptyView.show(false, "没有任何通知", null, null, null);
        } else {
            List<NoticeLocaleBean> dataBeans = DataBaseManager.get().queryNoticeByUser(StringHelper.getUserBean().getId());
            if (dataBeans.size() == 0) {
                getBinding().emptyView.show(false, "没有任何通知", null, null, null);
            } else {
                getBinding().emptyView.hide();
                adapter = new NormalRecyclerAdapter<NoticeLocaleBean>(R.layout.item_notice_rv_l, dataBeans) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, NoticeLocaleBean item) {
                        viewHolder.setText(R.id.dateView, item.getPushDate())
                                .setText(R.id.titleView, item.getTitle())
                                .setText(R.id.contentView, item.getContent());
                        if (item.getIsRead().equals("0")) {
                            viewHolder.setVisibility(R.id.readStateView, View.GONE);
                        } else {
                            viewHolder.setVisibility(R.id.readStateView, View.VISIBLE);
                        }

                        viewHolder.setOnClickListener(R.id.deleteView, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        DataBaseManager.get().deleteNoticeByMessageId(item.getMessageId());
                                        dataBeans.remove(position);
                                        adapter.notifyItemRemoved(position);
                                        adapter.notifyItemRangeChanged(position, dataBeans.size() - position);
                                    }
                                })
                                .setOnClickListener(R.id.showDetailLayout, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        String caseId = item.getCasePrimaryKey();
                                        ContextKit.navigatePageTo(requireContext(), CaseDetailActivity.class, caseId);
                                        //更改数据库里通知状态
                                        DataBaseManager.get().updateNoticeByMessageId(item.getMessageId());
                                        adapter.notifyItemChanged(position);
                                        adapter.notifyItemRangeChanged(position, dataBeans.size() - position);
                                    }
                                });
                    }
                };
                getBinding().noticeRecyclerView.setAdapter(adapter);
            }
        }
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initEvent() {

    }
}