package com.bixolon.sample;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.bixolon.BixolonConst;
import com.bixolon.commonlib.BXLCommonConst;
import com.bixolon.commonlib.common.BXLHelper;
import com.bixolon.commonlib.log.LogService;
import com.bixolon.mpos.MPosControllerBCD;
import com.bixolon.mpos.MPosControllerConfig;
import com.bixolon.mpos.MPosControllerDallasKey;
import com.bixolon.mpos.MPosControllerHID;
import com.bixolon.mpos.MPosControllerLabelPrinter;
import com.bixolon.mpos.MPosControllerMSR;
import com.bixolon.mpos.MPosControllerPrinter;
import com.bixolon.mpos.MPosControllerRFID;
import com.bixolon.mpos.MPosControllerScale;
import com.bixolon.mpos.MPosControllerScanner;
import com.bixolon.mpos.MPosControllerTTYUSB;
import com.bixolon.mpos.MPosControllerUSBSerial;
import com.bixolon.mpos.event.DataEvent;
import com.bixolon.mpos.event.DataListener;
import com.bixolon.mpos.event.StatusUpdateEvent;
import com.bixolon.mpos.event.StatusUpdateListener;

public class MainActivity extends AppCompatActivity implements StatusUpdateListener, DataListener {
    private static Fragment currentFragment;
    private static int currentPosition = 0;

    private Toolbar toolbar;
    private TabLayout mTabLayout = null;
    private ViewPager mViewPager = null;
    private static TabPagerAdapter mPagerAdapter = null;

    public static MPosControllerPrinter mPosControllerPrinter = new MPosControllerPrinter();
    public static MPosControllerLabelPrinter mPosControllerLabelPrinter = new MPosControllerLabelPrinter();
    public static MPosControllerHID mPosControllerHID = new MPosControllerHID();
    public static MPosControllerMSR mPosControllerMSR = new MPosControllerMSR();
    public static MPosControllerScanner mPosControllerScanner = new MPosControllerScanner();
    public static MPosControllerRFID mPosControllerRFID = new MPosControllerRFID();
    public static MPosControllerDallasKey mPosControllerDallasKey = new MPosControllerDallasKey();
    public static MPosControllerTTYUSB mPosControllerTTYUSB = new MPosControllerTTYUSB();
    public static MPosControllerBCD mPosControllerBCD = new MPosControllerBCD();
    public static MPosControllerUSBSerial mPosControllerUSBSerial = new MPosControllerUSBSerial();
    public static MPosControllerScale mPosControllerScale = new MPosControllerScale();
    public static MPosControllerConfig mPosControllerConfig = new MPosControllerConfig();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mTabLayout = findViewById(R.id.main_tab);
        mViewPager = findViewById(R.id.main_viewpager);
        mTabLayout.setupWithViewPager(mViewPager);

        mPagerAdapter = new TabPagerAdapter(this.getSupportFragmentManager(), this);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                currentPosition = tab.getPosition();
            }

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

            }

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

            }
        });

        if (Build.VERSION.SDK_INT >= 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        mPosControllerPrinter.addStatusUpdateListener(this);
        mPosControllerPrinter.addDataListener(this);
        mPosControllerLabelPrinter.addStatusUpdateListener(this);
        mPosControllerLabelPrinter.addDataListener(this);
        mPosControllerHID.addStatusUpdateListener(this);
        mPosControllerHID.addDataListener(this);
        mPosControllerMSR.addStatusUpdateListener(this);
        mPosControllerMSR.addDataListener(this);
        mPosControllerScanner.addStatusUpdateListener(this);
        mPosControllerScanner.addDataListener(this);
        mPosControllerRFID.addStatusUpdateListener(this);
        mPosControllerRFID.addDataListener(this);
        mPosControllerDallasKey.addStatusUpdateListener(this);
        mPosControllerDallasKey.addDataListener(this);
        mPosControllerTTYUSB.addStatusUpdateListener(this);
        mPosControllerTTYUSB.addDataListener(this);
        mPosControllerBCD.addStatusUpdateListener(this);
        mPosControllerBCD.addDataListener(this);
        mPosControllerUSBSerial.addStatusUpdateListener(this);
        mPosControllerUSBSerial.addDataListener(this);
        mPosControllerScale.addStatusUpdateListener(this);
        mPosControllerScale.addDataListener(this);
    }

    @Override
    protected void onDestroy() {
        mPosControllerPrinter.removeStatusUpdateListener(this);
        mPosControllerPrinter.removeDataListener(this);
        mPosControllerLabelPrinter.removeStatusUpdateListener(this);
        mPosControllerLabelPrinter.removeDataListener(this);
        mPosControllerHID.removeStatusUpdateListener(this);
        mPosControllerHID.removeDataListener(this);
        mPosControllerMSR.removeStatusUpdateListener(this);
        mPosControllerMSR.removeDataListener(this);
        mPosControllerScanner.removeStatusUpdateListener(this);
        mPosControllerScanner.removeDataListener(this);
        mPosControllerRFID.removeStatusUpdateListener(this);
        mPosControllerRFID.removeDataListener(this);
        mPosControllerDallasKey.removeStatusUpdateListener(this);
        mPosControllerDallasKey.removeDataListener(this);
        mPosControllerTTYUSB.removeStatusUpdateListener(this);
        mPosControllerTTYUSB.removeDataListener(this);
        mPosControllerBCD.removeStatusUpdateListener(this);
        mPosControllerBCD.removeDataListener(this);
        mPosControllerUSBSerial.removeStatusUpdateListener(this);
        mPosControllerUSBSerial.removeDataListener(this);
        mPosControllerScale.removeStatusUpdateListener(this);
        mPosControllerScale.removeDataListener(this);

        mPosControllerPrinter.closeService(5);
        mPosControllerLabelPrinter.closeService(5);
        mPosControllerHID.closeService(5);
        mPosControllerMSR.closeService(5);
        mPosControllerScanner.closeService(5);
        mPosControllerRFID.closeService(5);
        mPosControllerDallasKey.closeService(5);
        mPosControllerTTYUSB.closeService(5);
        mPosControllerBCD.closeService(5);
        mPosControllerUSBSerial.closeService(5);
        mPosControllerScale.closeService(5);
        mPosControllerConfig.closeService(5);

        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //return super.onCreateOptionsMenu(menu);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //return super.onOptionsItemSelected(item);
        return true;
    }

    @Override
    public void statusUpdateOccurred(StatusUpdateEvent statusUpdateEvent) {
        mHandler.obtainMessage(0, statusUpdateEvent).sendToTarget();
    }

    @Override
    public void dataOccurred(DataEvent dataEvent) {
        mHandler.obtainMessage(1, dataEvent).sendToTarget();
    }

    public final Handler mHandler = new Handler(new Handler.Callback() {
        @SuppressWarnings("unchecked")
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    StatusUpdateEvent statusUpdateEvent = (StatusUpdateEvent) msg.obj;
                    Object obj = statusUpdateEvent.getSource();
                    int status = statusUpdateEvent.getStatus();
                    String statusMsg = "NORMAL(IDLE)";
                    if(obj instanceof MPosControllerPrinter) {
                        // POS/Mobile Printer
                        if (status == BixolonConst.MPOS_PRINTER_OFFLINE) statusMsg = "RINTER_OFFLINE";
                        if (status == BixolonConst.MPOS_PRINTER_ONLINE) statusMsg = "PRINTER_ONLINE";
                        if (status == BixolonConst.MPOS_STATUS_PRINTER_COVEROPEN) statusMsg = "PRINTER_COVEROPEN";
                        if (status == BixolonConst.MPOS_STATUS_PRINTER_PAPEREMPTY) statusMsg = "PRINTER_PAPEREMPTY";
                        if (status == BixolonConst.MPOS_STATUS_PRINTER_ERROR) statusMsg = "PRINTER_ERROR";
                        if (status == BixolonConst.MPOS_STATUS_PRINTER_PAPER_NEAREND) statusMsg = "PAPER_NEAREND";
                        if (status == BixolonConst.MPOS_STATUS_BATT_LOW) statusMsg = "BATT_LOW";
                    } else if(obj instanceof MPosControllerLabelPrinter) {
                        // Label Printer
                        if (status == BixolonConst.MPOS_PRINTER_OFFLINE) statusMsg = "RINTER_OFFLINE";
                        if (status == BixolonConst.MPOS_PRINTER_ONLINE) statusMsg = "PRINTER_ONLINE";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_COVEROPEN) statusMsg = "PRINTER_COVEROPEN";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_PAPEREMPTY) statusMsg = "PRINTER_PAPEREMPTY";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_CUTTER_JAMMED) statusMsg = "PRINTER_CUTTER_JAMMED";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_TPH_OVERHEAT) statusMsg = "PRINTER_TPH_OVERHEAT";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_GAP_ERROR) statusMsg = "PRINTER_GAP_ERROR";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_RIBBON_END) statusMsg = "PAPER_RIBBON_END";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_ON_BUILDING_LABEL_TO_BE_PRINTED) statusMsg = "PRINTER_ON_BUILDING_LABEL_TO_BE_PRINTED";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_ON_PRINTING_LABEL) statusMsg = "PRINTER_ON_PRINTING_LABEL";
                        if (status == BixolonConst.MPOS_LABEL_STATUS_PRINTER_ISSUED_LABEL_ISPAUSED) statusMsg = "PAPER_ISSUED_LABEL_ISPAUSED";
                    } else {
                        // etc device
                        if (status == BixolonConst.MPOS_PRINTER_OFFLINE) statusMsg = "RINTER_OFFLINE";
                        if (status == BixolonConst.MPOS_PRINTER_ONLINE) statusMsg = "PRINTER_ONLINE";
                    }

                    Toast.makeText(getApplicationContext(), statusMsg + " : " + status, Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    DataEvent dataEvent = (DataEvent) msg.obj;
                    byte[] data = (byte[]) dataEvent.getObject();
                    if(data == null || data.length == 0) {
                        return false;
                    }

                    String hexString = BXLHelper.Hex2HexStr(data, data.length, true, true);
                    Toast.makeText(getApplicationContext(), hexString, Toast.LENGTH_LONG).show();
                    break;
            }
            return false;
        }
    });
}
