package com.gingersoft.gsa.cloud.table.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.gingersoft.gsa.cloud.base.utils.VibratorUtils;
import com.gingersoft.gsa.cloud.base.common.bean.TableBean;
import com.gingersoft.gsa.cloud.table.app.TableOperatTypeConstant;
import com.gingersoft.gsa.cloud.table.mvp.ui.activity.OrderContentActivity;
import com.gingersoft.gsa.cloud.table.mvp.ui.activity.TableActivity;
import com.gingersoft.gsa.cloud.table.mvp.ui.adapter.TableAdapter;
import com.gingersoft.gsa.cloud.table.mvp.ui.fragment.AllTableFragment;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.gingersoft.gsa.cloud.table.mvp.contract.AllTableContract;
import com.jess.arms.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/28/2019 17:33
 * <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>
 * ================================================
 */
@FragmentScope
public class AllTablePresenter extends BasePresenter<AllTableContract.Model, AllTableContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private TableActivity mTableActivity;
    private OrderContentActivity mOrderContentActivity;

    private AllTableFragment IView;

    private TableAdapter mTableAdapter;
    private List<TableBean.DataBean> mTableList = new ArrayList<>();


    @Inject
    public AllTablePresenter(AllTableContract.Model model, AllTableContract.View rootView) {
        super(model, rootView);
        this.IView = (AllTableFragment) rootView;

        Activity activity = IView.getCurrentActivity();
        if (activity instanceof TableActivity) {
            mTableActivity = (TableActivity) activity;
        } else if (activity instanceof OrderContentActivity) {
            mOrderContentActivity = (OrderContentActivity) activity;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void initTableAdapter(Context context) {
        if (mTableAdapter == null) {
            mTableAdapter = new TableAdapter(context, mTableList, DeviceUtils.getScreenHeight(mApplication), TableActivity.FUNCTION_ALL_TABLE);
            mTableAdapter.setStyle(0);
            mRootView.setAllTableAdapter(mTableAdapter);
            initItemClickListener(context);
        }
    }

    private void initItemClickListener(Context context) {
        mTableAdapter.setOnItemClickListener(new TableAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(TableBean.DataBean item, int position) {

                VibratorUtils.pressButtonSound(mApplication);

                if (mTableActivity != null) {
                    mTableActivity.clickTableItem(item);
                } else if (mOrderContentActivity != null) {
                    mOrderContentActivity.clickTableItem(item);
                }
            }
        });
    }

    private void initTableByZoneId(List<TableBean.DataBean> tableList) {
//        int zoneId = GSAApplication.tableContract.getZoneid();
//        if (zoneId == -1) {
        mTableList.addAll(tableList);
//        } else {
//            for (TableBean.DataBean datasBean : tableList) {
//                // 获取当前ID的区域
//                if (datasBean.getZoneid() == zoneId) {
//                    mTableList.add(datasBean);
//                }
//            }
//        }
    }

    /**
     * 更新全部頁餐檯數據
     *
     * @param context
     * @param tableList
     */
    public void updateAllTableData(Context context, List<TableBean.DataBean> tableList) {
        if (tableList != null) {
            mTableList.clear();
            initTableByZoneId(tableList);
            initTableAdapter(context);
            mTableAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 更新狀態頁餐檯數據
     *
     * @param context
     * @param tableList
     */
    public void updateStateTableData(Context context, List<TableBean.DataBean> tableList, int status) {
        if (tableList != null) {
            //这里先清空
            mTableList.clear();
            if (status == TableOperatTypeConstant.table_status_0) {
                //显示全部台号 按照狀態排序
                initTableByStauts(tableList);
            } else {
                //根据状态只显示相应状态台号
                filterTableByTableStatus(tableList, status);
            }
            initTableAdapter(context);
            mTableAdapter.notifyDataSetChanged();
        }

    }

    /**
     * 根据餐台状态 过滤餐台
     * 0#全部
     * 1#可使用
     * 2#已使用
     * 3#已送單
     * 4#已印單
     * 5#已分檯
     */
    public void filterTableByTableStatus(List<TableBean.DataBean> datasBeans, int status) {
        if (datasBeans.size() == 0) {
            return;
        }
        switch (status) {
            case TableOperatTypeConstant.table_status_1:
                for (int i = 0; i < datasBeans.size(); i++) {
                    if (datasBeans.get(i).getStatus() == 0 && datasBeans.get(i).getUseStatus() == 0) {
                        mTableList.add(datasBeans.get(i));
                    }
                }
                break;
            case TableOperatTypeConstant.table_status_2:
                for (int i = 0; i < datasBeans.size(); i++) {
                    if (datasBeans.get(i).getUseStatus() == 1) {
                        mTableList.add(datasBeans.get(i));
                    }
                }
                break;
            case TableOperatTypeConstant.table_status_3:
                for (int i = 0; i < datasBeans.size(); i++) {
                    if ((datasBeans.get(i).getStatus() == 1 || datasBeans.get(i).getStatus() == 2) && datasBeans.get(i).getUseStatus() == 0) {
                        mTableList.add(datasBeans.get(i));
                    }
                }
                break;
            case TableOperatTypeConstant.table_status_4:
                for (int i = 0; i < datasBeans.size(); i++) {
                    if (datasBeans.get(i).getStatus() == 3 && datasBeans.get(i).getUseStatus() == 0) {
                        mTableList.add(datasBeans.get(i));
                    }
                }
                break;
            default:
                mTableList.addAll(datasBeans);
                break;
        }
    }


    private void initTableByStauts(List<TableBean.DataBean> tableList) {

        List<TableBean.DataBean> tb_yellow = new ArrayList<>();
        List<TableBean.DataBean> tb_green = new ArrayList<>();
        List<TableBean.DataBean> tb_red = new ArrayList<>();
        List<TableBean.DataBean> tb_white = new ArrayList<>();
        List<TableBean.DataBean> tb_w6 = new ArrayList<>();
        List<TableBean.DataBean> tb_unkown = new ArrayList<>();
        //黃色=状态=3 , 綠色=2, 紅色=using=1, 白色=0

        for (TableBean.DataBean datasBean : tableList) {
            if (datasBean.getUseStatus() == 1) {
                tb_red.add(datasBean);
            } else {
                switch (datasBean.getStatus()) {
                    case 0:
                        tb_white.add(datasBean);
                        break;
                    case 2:
                        tb_green.add(datasBean);
                        break;
                    case 3:
                        tb_yellow.add(datasBean);
                        break;
                    case 6:
                        tb_w6.add(datasBean);
                        break;
                    default:
                        tb_unkown.add(datasBean);
                        break;
                }
            }
        }
        mTableList.addAll(tb_yellow);
        mTableList.addAll(tb_green);
        mTableList.addAll(tb_red);
        mTableList.addAll(tb_w6);
        mTableList.addAll(tb_unkown);
        mTableList.addAll(tb_white);
    }


    /**
     * 更新状态以改变餐台
     *
     * @param newTableList
     */
    public void updataTableItem(List<TableBean.DataBean> newTableList) {
//        for (int i = 0; i < newTableList.size(); i++) {
//            TableBean.DataBean newTableBean = newTableList.get(i);
//            for (int j = 0; j < mTableList.size(); j++) {
//                TableBean.DataBean tableBean = mTableList.get(j);
//                if (tableBean.getTableno() == newTableBean.getTableno()) {
//                    tableBean.setOrderid(newTableBean.getOrderid());
//                    tableBean.setOrderid_1(newTableBean.getOrderid_1());
//                    tableBean.setStatus(newTableBean.getStatus());
//                    tableBean.setUsing(newTableBean.isUsing());
//                    tableBean.setMainshowtable(newTableBean.getMainshowtable());
//                    tableBean.setSplitStatus(newTableBean.getSplitStatus());
//                    tableBean.setOropentime_diff(newTableBean.getOropentime_diff());
//                    tableBean.setPrtime_diff(newTableBean.getPrtime_diff());
//                    tableBean.setClearstate(newTableBean.getClearstate());
//                    tableBean.setOpentime(newTableBean.getOpentime());
//                    tableBean.setMemid(newTableBean.getMemid());
//                    tableBean.setSc_rate(newTableBean.getSc_rate());
//                    tableBean.setPax(newTableBean.getPax());
//                    tableBean.setQr_code(newTableBean.getQr_code());
//                    tableBean.setRiceponorderid(newTableBean.getRiceponorderid());
//
//                    mTableAdapter.notifyItemChanged(j);
//                }
//            }
//        }
    }
}
