package hf.lskj.com.carstockcount.activity;

import android.media.AudioManager;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import hf.lskj.com.carstockcount.R;
import hf.lskj.com.carstockcount.event.ChooseCarEvent;
import hf.lskj.com.carstockcount.event.ChooseRecordEvent;
import hf.lskj.com.carstockcount.event.ExitEvent;
import hf.lskj.com.carstockcount.fragment.BindFragment;
import hf.lskj.com.carstockcount.fragment.MeFragment;
import hf.lskj.com.carstockcount.fragment.StockCountFragment;
import hf.lskj.com.carstockcount.utils.ToastUtils;
import hf.lskj.com.carstockcount.utils.UHF.ShellUtils;
import hf.lskj.com.carstockcount.utils.UHF.UHFClient;

public class MainActivity extends BaseActivity {
    public static String open_1 = "echo 1 > /sys/devices/soc.0/xt_dev.68/xt_uhf_en";
    public static String open_2 = "echo 0 >/sys/devices/soc.0/xt_dev.68/xt_uart_a";
    public static String open_3 = "echo 1 >/sys/devices/soc.0/xt_dev.68/xt_uart_b";

    public static String close_1 = "echo 0 > /sys/devices/soc.0/xt_dev.68/xt_uhf_en";
    public static String close_2 = "echo 0 >/sys/devices/soc.0/xt_dev.68/xt_uart_a";
    public static String close_3 = "echo 0 >/sys/devices/soc.0/xt_dev.68/xt_uart_b";
    private FragmentPagerAdapter pagerAdapter;
    private ViewPager mViewPager;
    private TabLayout.Tab one, two, three;
    private StockCountFragment stockCountFragment;
    private BindFragment bindFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("RFID智能盘库管理", false);
        initViews();
        Boolean ret = ShellUtils.checkRootPermission();
        Log.e("rfid", "ret=" + ret);
        ShellUtils.execCommand(open_1, ret);
        ShellUtils.execCommand(open_2, ret);
        ShellUtils.execCommand(open_3, ret);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    private void initViews() {
        TabLayout mTablayout = (TabLayout) findViewById(R.id.tabLayout);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            private String[] mTitles = new String[]{"关联标签", "盘点", "我"};

            @Override
            public Fragment getItem(int position) {
                if (position == 1) {
                    stockCountFragment = new StockCountFragment();
                    return stockCountFragment;
                } else if (position == 2) {
                    return new MeFragment();
                } else {
                    bindFragment = new BindFragment();
                    return bindFragment;
                }
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }

        };
        mViewPager.setAdapter(pagerAdapter);

        mTablayout.setupWithViewPager(mViewPager);

        one = mTablayout.getTabAt(0);
        two = mTablayout.getTabAt(1);
        if (two != null) {
            two.select();
        }
        three = mTablayout.getTabAt(2);

        one.setIcon(getResources().getDrawable(R.drawable.guanlian));
        two.setIcon(getResources().getDrawable(R.drawable.pandian));
        three.setIcon(getResources().getDrawable(R.drawable.user));
        mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (tab.getPosition() == 1) {
                    stockCountFragment.setCountRecord(null);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    private long exitTime = 0;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        closeUHF();
    }

    /**
     * 断开连接 关闭电源
     **/
    public static void closeUHF() {
        UHFClient.Disconnect();
        Boolean ret = ShellUtils.checkRootPermission();
        Log.e("TAG", "ret=" + ret);
        ShellUtils.execCommand(close_1, ret);
        ShellUtils.execCommand(close_2, ret);
        ShellUtils.execCommand(close_3, ret);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if ((System.currentTimeMillis() - exitTime) > 1500) {
                        ToastUtils.showShort("再按一次退出程序");
                        exitTime = System.currentTimeMillis();
                    } else {
                        EventBus.getDefault().post(new ExitEvent());
                    }
                    return true;
                case 224://scan键
                    switch (mViewPager.getCurrentItem()) {
                        case 0:
                            bindFragment.onKeyDown(keyCode, event);
                            break;
                        case 1:
                            stockCountFragment.onKeyDown(keyCode, event);
                            break;
                    }
                    break;
                case 131://F1
                    break;
                case 132://F2
                    break;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Subscribe()
    public void onMessageEvent(ChooseRecordEvent event) {
        if (event.record != null) {
            two.select();
            stockCountFragment.setCountRecord(event.record);//查看盘点记录
        }
    }

    @Subscribe()
    public void onMessageEvent(ChooseCarEvent event) {
        if (event.carInfo != null) {
            if (TextUtils.isEmpty(event.carInfo.getRfid())) {
                one.select();
                bindFragment.setCarInfo(event.carInfo);//去关联标签
            } else {
                two.select();
                stockCountFragment.setCarInfo(event.carInfo);//去做异常盘点
            }
        }
    }
}
