package com.gingersoft.supply_chain.mvp.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.gingersoft.gsa.cloud.database.bean.Function;
import com.gingersoft.supply_chain.R;
import com.gingersoft.supply_chain.R2;
import com.gingersoft.supply_chain.di.component.DaggerFunctionListComponent;
import com.gingersoft.supply_chain.mvp.bean.PurchaseFunctionBean;
import com.gingersoft.supply_chain.mvp.contract.FunctionListContract;
import com.gingersoft.supply_chain.mvp.presenter.FunctionListPresenter;
import com.gingersoft.supply_chain.mvp.ui.adapter.FunctionChildAdapter;
import com.gingersoft.supply_chain.mvp.ui.adapter.PurchaseFunctionAdapter;
import com.jess.arms.di.component.AppComponent;
import com.qmuiteam.qmui.widget.QMUITopBar;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 11/19/2020 16:15
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class FunctionListFragment extends BaseSupplyChainFragment<FunctionListPresenter> implements FunctionListContract.View {

    @BindView(R2.id.topbar_supply_chain)
    QMUITopBar mTopBar;
    @BindView(R2.id.rv_supply_chain_function_list)
    RecyclerView rvSupplyChainFunctionList;

    public static FunctionListFragment newInstance() {
        FunctionListFragment fragment = new FunctionListFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerFunctionListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_function_list, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initTopBar();

        List<PurchaseFunctionBean> purchaseFunctionBeans = new ArrayList<>();

        List<Function> basicManagementFunction = new ArrayList<>();
        basicManagementFunction.add(new Function("供應商", R.drawable.ic_supplier));
        basicManagementFunction.add(new Function("食材", R.drawable.ic_food_ingredients));
        basicManagementFunction.add(new Function("種類", R.drawable.ic_type));
        basicManagementFunction.add(new Function("包裝", R.drawable.ic_packing));
        purchaseFunctionBeans.add(new PurchaseFunctionBean("基礎管理", basicManagementFunction));

        List<Function> orderManger = new ArrayList<>();
        orderManger.add(new Function("採購訂單", R.drawable.ic_purchase_order));
        purchaseFunctionBeans.add(new PurchaseFunctionBean("訂單管理", orderManger));
        PurchaseFunctionAdapter purchaseFunctionAdapter = new PurchaseFunctionAdapter(mContext, purchaseFunctionBeans);
        purchaseFunctionAdapter.setFunctionClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                FunctionChildAdapter functionChildAdapter = (FunctionChildAdapter) adapter;
                List<Function> data = functionChildAdapter.getData();
                switch (data.get(position).getResName()) {
                    case "供應商":
                        start(SupplierListFragment.newInstance());
                        break;
                    case "包裝":
                        start(MeasurementUnitFragment.newInstance());
                        break;
                    default:

                        break;
                }
            }
        });
        rvSupplyChainFunctionList.setAdapter(purchaseFunctionAdapter);
    }

    private void initTopBar() {
        mTopBar.setTitle("採購");
        mTopBar.addLeftBackImageButton().setOnClickListener(v -> killMyself());
    }

}
