package com.bixolon.sample;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bixolon.BixolonConst;
import com.bixolon.mpos.config.util.MPosControllerLookup;

public class ScaleFragment extends Fragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroupPortType;

    private EditText editTextDevID = null;
    private EditText editTextAddress = null;

    private int portType = BixolonConst.MPOS_INTERFACE_BLUETOOTH;

    public static ScaleFragment newInstance() {
        ScaleFragment fragment = new ScaleFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scale, container, false);

        radioGroupPortType = view.findViewById(R.id.radioGroupPortType);
        radioGroupPortType.setOnCheckedChangeListener(this);

        editTextDevID = view.findViewById(R.id.editTextDevID);
        editTextAddress = view.findViewById(R.id.editTextAddress);
        editTextAddress.setText("74:F0:7D:10:10:01");

        view.findViewById(R.id.btnGetAddress).setOnClickListener(this);
        view.findViewById(R.id.btnOpen).setOnClickListener(this);
        view.findViewById(R.id.btnClose).setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnGetAddress:
                try {
                    String[] items = null;
                    if (BixolonConst.MPOS_SUCCESS == MPosControllerLookup.refreshDeviceList(portType, 3)) {
                        MPosControllerLookup.MPosDeviceInfo[] deviceInfos = MPosControllerLookup.getDeviceList(portType);
                        if (deviceInfos != null && deviceInfos.length > 0) {
                            items = new String[deviceInfos.length];
                            if (portType == BixolonConst.MPOS_INTERFACE_BLUETOOTH) {
                                for (int i = 0; i < deviceInfos.length; i++) {
                                    items[i] = deviceInfos[i].deviceName + "\n" + deviceInfos[i].macAddress;
                                }
                            } else if (portType == BixolonConst.MPOS_INTERFACE_WIFI) {
                                for (int i = 0; i < deviceInfos.length; i++) {
                                    items[i] = deviceInfos[i].ipAddress;
                                }
                            }

                            final String[] finalItems = items;
                            new AlertDialog.Builder(getContext()).setTitle("Device List")
                                    .setItems(items, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            String strSelectList = finalItems[which];
                                            String strDeviceInfo = null;
                                            if (portType == BixolonConst.MPOS_INTERFACE_BLUETOOTH) {
                                                String[] splitAddr = strSelectList.split("\n");
                                                strDeviceInfo = splitAddr[1];
                                            } else {
                                                strDeviceInfo = strSelectList;
                                            }
                                            editTextAddress.setText(strDeviceInfo);
                                        }
                                    }).show();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btnOpen:
                openService();
                break;
            case R.id.btnClose:
                closeService();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radioBT:
                editTextAddress.setText("74:F0:7D:00:00:00");
                portType = BixolonConst.MPOS_INTERFACE_BLUETOOTH;
                break;
            case R.id.radioWifi:
                editTextAddress.setText("192.168.0.1");
                portType = BixolonConst.MPOS_INTERFACE_WIFI;
                break;
            case R.id.radioUSB:
                editTextAddress.setText("");
                portType = BixolonConst.MPOS_INTERFACE_USB;
                break;
        }
    }

    private void openService() {
        String strId = editTextDevID.getText().toString();
        String strAddress = editTextAddress.getText().toString();
        if (strId.isEmpty() || strAddress.isEmpty()) {
            return;
        }

        int devId = Integer.parseInt(strId);

        MainActivity.mPosControllerScale.selectCommandMode(BixolonConst.MPOS_COMMAND_MODE_DEFAULT);
        MainActivity.mPosControllerScale.selectInterface(portType, strAddress);
        long ret = MainActivity.mPosControllerScale.openService(devId, getContext());
        if(ret != BixolonConst.MPOS_SUCCESS) {
            Toast.makeText(getContext(), "Device open fail : " + ret, Toast.LENGTH_LONG).show();
            return;
        }
    }

    private void closeService() {
        if(MainActivity.mPosControllerScale.isOpen()) {
            MainActivity.mPosControllerScale.closeService(0);
        }
    }
}
