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

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

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.linktab.LinkMode;
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.ImageButtonControl;
import com.bonait.bnframework.modules.home.fragment.mode.Silos_item_fragment;
import com.bonait.bnframework.modules.home.fragment.mode.add_silos_ck;
import com.bonait.bnframework.modules.home.fragment.mode.add_yijian_controlextends;
import com.bonait.bnframework.modules.home.fragment.mode.silos_jiaoyan;
import com.qmuiteam.qmui.arch.QMUIFragment;
import com.qmuiteam.qmui.arch.QMUIFragmentPagerAdapter;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SilosNewActivity extends BaseActivity {

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

    @BindView(R.id.tabs)
    QMUITabSegment tabs;//料仓分类

    @BindView(R.id.viewpager_silos)
    QMUIViewPager viewpager_silos;//料仓

    /**
     * 查看料仓
     */
    @BindView(R.id.add_silos)
    add_silos_ck add_silos;

    /**
     * 一键设置等
     */
    @BindView(R.id.add_yijian)
    add_yijian_controlextends add_yijian;

    /**
     * 料仓校验
     */
    @BindView(R.id.silos_jz)
    silos_jiaoyan silos_jz;

    @BindView(R.id.yijian3)
    ImageButtonControl yijian3;

    private Context context;

    Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_silos_new);
        activity=this;
        ButterKnife.bind(this);
        context = getContext();
        initTopBar();
        InitData();
    }

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

        //一键控制
        add_yijian.mListener = new MyClickListener() {
            @Override
            public void clickListener(View v, Object data) {

            }

            @Override
            public void clickListenerNew(View v, int k, Object data) {
                if (k == 0) {
                    DataBus.getInstance().RefreshSilos();
                    MessageManager.getInstance().sendMessage(MessageName.RefreshSilos,"RefreshSilos");
                    add_yijian.setVisibility(View.GONE);
                } else if (k == 1) {
                } else if (k == 2) {
                } else if (k == 3) {
                } else if (k == 4) {
                }
            }
        };

        MessageManager.getInstance().registerMessageReceiver(this, MessageName.ClickSilos, new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    add_silos.SetData((lcMode) msg);
                    add_silos.setVisibility(View.VISIBLE);
                }
            }
        });

        //料仓设置
        add_silos.mListener = new MyClickListener() {
            @Override
            public void clickListener(View v, Object data) {
            }

            @Override
            public void clickListenerNew(View v, int k, Object data) {
                if (k == 0) {
                    DataBus.getInstance().RefreshSilos();
                    MessageManager.getInstance().sendMessage(MessageName.RefreshSilos,"RefreshSilos");
                    add_silos.setVisibility(View.GONE);
                } else if (k == 1) {
                    ToastUtils.info("补充原料成功!!!");
                } else if (k == 2) {
                    //满贯
                } else if (k == 3) {
                    //设置
                } else if (k == 4) {
                    //校准
                    silos_jz.SetData((lcMode) data, activity);
                    silos_jz.setVisibility(View.VISIBLE);
                } else if (k == 5) {
                    //
                } else if (k == 6) {
                    ToastUtils.info("设置物料成功!!!");
                }
            }
        };

        //料仓校准
        silos_jz.mListener = new MyClickListener() {
            @Override
            public void clickListener(View v, Object data) {
            }

            @Override
            public void clickListenerNew(View v, int k, Object data) {
                if (k == 0) {
                    silos_jz.setVisibility(View.GONE);
                }
            }
        };
    }

    //list 界面
    /**
     * 初始化数据
     */
    public void InitData() {
        //1.刷新料仓
        DataBus.getInstance().RefreshSilos();
        QMUIFragmentPagerAdapter adapter = new QMUIFragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return DataBus.getInstance().SilosData.size();
            }
            @Override
            public QMUIFragment createFragment(int position) {
                Silos_item_fragment fragment = new Silos_item_fragment();
                fragment.SetData(position,activity);
                return fragment;
            }
            @Override
            public CharSequence getPageTitle(int position) {
                return DataBus.getInstance().SilosData.get(position).type.toString();
            }
        };
        viewpager_silos.setAdapter(adapter);
        tabs.setupWithViewPager(viewpager_silos, true);

        if (ConfigName.getInstance().versionSelectionEnum.equals("配料吧台"))
        {
            yijian3.setVisibility(View.GONE);
        }else
        {
            yijian3.setVisibility(View.VISIBLE);
        }
    }


    @OnClick({R.id.yijian1, R.id.yijian2, R.id.yijian3, R.id.yijian4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.yijian1:
                add_yijian.SetData(1);
                add_yijian.setVisibility(View.VISIBLE);
                break;
            case R.id.yijian2:
                add_yijian.SetData(2);
                add_yijian.setVisibility(View.VISIBLE);
                break;
            case R.id.yijian3:
                skipToActivity(ZdqxActivity.class);
                break;
            case R.id.yijian4:
                add_yijian.SetData(4);
                add_yijian.setVisibility(View.VISIBLE);
                break;
        }
    }

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

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