package com.example.bpa.view.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.bpa.R;
import com.example.bpa.config.DataBus;
import com.example.bpa.config.MessageName;
import com.example.bpa.helper.WrapContentLinearLayoutManager;
import com.example.bpa.message.MessageLooper;
import com.example.bpa.message.MessageManager;
import com.example.bpa.view.adapter.maingoods_adapter;
import com.example.bpa.view.adapter.makegood_adapter;
import com.example.bpa.view.control.MyLayoutManager;
import com.example.bpa.view.inteface.MyClickListener;

/**
 * 主页界面
 */
public class HomeFragment extends Fragment implements View.OnClickListener, MyClickListener {

    //region 变量
    /**
     * 商品列表
     */
    RecyclerView good_recyclerView;
    /**
     * 商品制作列表
     */
    RecyclerView MakeGood;
    /**
     * 界面实例
     */
    public View view;
    //endregion

    //region 私有函数
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container,false);
        Init();
        return view;
    }
    //endregion

    //region 公共函数
    /**
     * 初始化
     */
    private void Init(){
        good_recyclerView = view.findViewById(R.id.good_recyclerView);
        MakeGood=view.findViewById(R.id.MakeGood);
        Initdata();//初始化数据
        RegisterMessage();//消息中心事件接收
    }
    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        activity=findActivity(view.getContext());
    }
    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }
    maingoods_adapter goodadapter;
    /**
     * 初始化数据加载
     */
    public void Initdata()
    {
        try{
            MyLayoutManager layout = new MyLayoutManager();
            layout.setAutoMeasureEnabled(true);
            good_recyclerView.setLayoutManager(layout);

             goodadapter = new maingoods_adapter( getContext());
            good_recyclerView.setAdapter(goodadapter);
            DataBus.getInstance().UpdateMainGoods();//更新商品


            MakeGood.setLayoutManager(new WrapContentLinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL,false));
            DataBus.getInstance().goodmakeadapter = new makegood_adapter(getContext());
            MakeGood.setAdapter(DataBus.getInstance().goodmakeadapter);
        }catch(Exception e){

        }
    }
    //endregion

    //region 点击事件
    /**
     * 本页面点击事件监听22
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonaddpf://新建配方

                break;
        }
    }

    /**
     * 接口方法，响应ListView按钮点击事件
     */
    @Override
    public void clickListener(View v,Object data) {
        switch (v.getId())
        {
            case R.id.button_item://删除按钮

                break;

        }
    }
    //endregion

    //region 接收事件
    Activity activity=null;
    public void RegisterMessage()
    {
        MessageManager.getInstance().registerMessageReceiver(this, MessageName.Common.name(), new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DataBus.getInstance().UpdateMainGoods();//更新商品
                        goodadapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
    //endregion
}