package com.bonait.bnframework.modules.home.fragment.from;

import static com.bonait.bnframework.MainApplication.getContext;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.message.MessageLooper;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.mode.QDListSectionAdapter;
import com.bonait.bnframework.modules.home.fragment.mode.SectionHeader;
import com.bonait.bnframework.modules.home.fragment.mode.SectionItem;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.section.QMUISection;
import com.qmuiteam.qmui.widget.section.QMUIStickySectionAdapter;
import com.qmuiteam.qmui.widget.section.QMUIStickySectionLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CpActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar mTopBar;

    @BindView(R.id.qmuisection_layout)
    QMUIStickySectionLayout mSectionLayout;
    private RecyclerView.LayoutManager mLayoutManager;
    protected static QMUIStickySectionAdapter<SectionHeader, SectionItem, QMUIStickySectionAdapter.ViewHolder> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cp);
        //属性绑定
        ButterKnife.bind(this);
        initTopBar();
        initAdapter();
        initAdapterLogicalOperation();
        NBClick();
    }

   // QDListSectionAdapter
    private void initAdapter() {
        mLayoutManager = createLayoutManager();
        mSectionLayout.setLayoutManager(mLayoutManager);
        mAdapter = createAdapter();
        //mSectionLayout.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.aword_fade_enter));
    }

    private void initAdapterLogicalOperation() {
        mAdapter.setCallback(new QMUIStickySectionAdapter.Callback<SectionHeader, SectionItem>() {
            @Override
            public void loadMore(final QMUISection<SectionHeader, SectionItem> section, final boolean loadMoreBefore) {
            }

            //点击事件
            @Override
            public void onItemClick(QMUIStickySectionAdapter.ViewHolder holder, int position) {
                if (holder.getItemViewType() != 1) {
                    int pos = holder.isForStickyHeader ? position : holder.getAdapterPosition();
                    mAdapter.toggleFold(pos, false);
                }
            }

            //长按事件
            @Override
            public boolean onItemLongClick(QMUIStickySectionAdapter.ViewHolder holder, int position) {
                int FirstPos = mAdapter.getSectionIndex(position);
                int SecondPos = mAdapter.getItemIndex(position);
                //ItemPopupWindow.show(getContext(), holder.itemView, holder.getItemViewType(), FirstPos, SecondPos);
                ToastUtils.info("一级条目" +FirstPos+ "二级"+ SecondPos);
                return true;
            }
        });
        mSectionLayout.setAdapter(mAdapter, true);
        mAdapter.setData(getList());
    }

    public void NBClick()
    {
        /**
         * 删除菜品
         */
        MessageManager.getInstance().registerMessageReceiver(this, MessageName.DeleteGood, new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    SectionItem item=(SectionItem)msg;
                    QueryDB.DeleteGoodsid(item.getAccount());
                    mAdapter.setData(getList());
                }
            }
        });

        /**
         * 刷新菜品
         */
        MessageManager.getInstance().registerMessageReceiver(this, MessageName.ScGood, new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    mAdapter.setData(new ArrayList<QMUISection<SectionHeader, SectionItem>>());
                    mAdapter.setData(getList());
                }
            }
        });

        /**
         * 点击菜品
         */
        MessageManager.getInstance().registerMessageReceiver(this, MessageName.ClickGood, new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    SectionItem item=(SectionItem)msg;
                    String goodid=item.getAccount();//商品id
                    Intent intent = new Intent(getContext(), DiyUpdateActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("id",goodid);
                    startActivity(intent);
                }
            }
        });
    }

    protected QMUIStickySectionAdapter<SectionHeader, SectionItem, QMUIStickySectionAdapter.ViewHolder> createAdapter() {
        return new QDListSectionAdapter();
    }

    protected RecyclerView.LayoutManager createLayoutManager() {
//        return new LinearLayoutManager(getContext()) {
//            @Override
//            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
//                return new RecyclerView.LayoutParams(
//                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            }
//        };
        final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return mAdapter.getItemIndex(i) < 0 ? layoutManager.getSpanCount() : 1;
            }
        });
        return layoutManager;
    }

    /**
     * 获取数据
     * @return
     */
    private static ArrayList<QMUISection<SectionHeader, SectionItem>> getList() {
        ArrayList<QMUISection<SectionHeader, SectionItem>> list = new ArrayList<>();
        ArrayList<BPA_GOODS> good= QueryDB.GetGoodsALL();

        SectionHeader header_bdcp = new SectionHeader("本地菜谱","","");
        ArrayList<SectionItem> contents_bdcp = new ArrayList<>();

        SectionHeader header_sccp = new SectionHeader("收藏菜谱","","");
        ArrayList<SectionItem> contents_sccp = new ArrayList<>();

        SectionHeader header_ydcp = new SectionHeader("云端菜谱","","");
        ArrayList<SectionItem> contents_ydcp = new ArrayList<>();

        for (BPA_GOODS item:good)
        {
            if(item.url==null)
            {
                item.url="未知";
            }
            contents_bdcp.add(new SectionItem(item.name, String.valueOf(item.maketime), item.id,item.url,item.issc==1));
            if(item.issc==1)
            {
                contents_sccp.add(new SectionItem(item.name, String.valueOf(item.maketime),  item.id,item.url,item.issc==1));
            }
        }
        list.add(new QMUISection<>(header_bdcp, contents_bdcp, false));
        list.add(new QMUISection<>(header_sccp, contents_sccp, true));
        list.add(new QMUISection<>(header_ydcp, contents_ydcp, true));
        return list;
    }

    private void initTopBar() {
        mTopBar.setTitle("菜谱管理");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected boolean canDragBack() {
        return false;
    }
}