package hr.fer.labust.diverapp.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import hr.fer.labust.diverapp.BluetoothService;
import hr.fer.labust.diverapp.R;

public class BluetoothListAdapter extends BaseAdapter {
    private final ArrayList<BluetoothDevice> mDevices;
    private final ArrayList<String> mDeviceNames;
    private final Context mContext;
    private final BluetoothService mBluetoothService;

    public BluetoothListAdapter(Context context, BluetoothService bluetoothService) {
        mDevices = new ArrayList<>();
        mDeviceNames = new ArrayList<>();
        mContext = context;
        mBluetoothService = bluetoothService;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final BluetoothDevice device = mDevices.get(position);

        if (convertView == null) {
            holder  = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_element_bluetooth, parent, false);
            holder.DeviceName = (TextView) convertView.findViewById(R.id.device_name);

            holder.ConnectButton = (Button) convertView.findViewById(R.id.connect_button);
            holder.ConnectButton.setOnClickListener(holder);

            holder.SendChatMessages = (CheckBox) convertView.findViewById(R.id.send_chat_messages_checkbox);
            holder.SendChatMessages.setOnCheckedChangeListener(holder);

            holder.SendCommandMessages = (CheckBox) convertView.findViewById(R.id.send_commands_checkbox);
            holder.SendCommandMessages.setOnCheckedChangeListener(holder);

            holder.SecureCheckBox = (CheckBox) convertView.findViewById(R.id.secure_checkbox);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.DeviceName.setText(device.getName());
        if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
            holder.DeviceName.setTypeface(null, Typeface.BOLD);
        }
        else {
            holder.DeviceName.setTypeface(null, Typeface.NORMAL);
        }

        if (mBluetoothService.isConnected(device)) {
            if (mBluetoothService.getState(device) == BluetoothService.STATE_CONNECTED) {
                holder.ConnectButton.setText(mContext.getString(R.string.disconnect));
                holder.SecureCheckBox.setVisibility(CheckBox.GONE);

                holder.SendCommandMessages.setChecked(mBluetoothService.getCommandFlag(device.getName()));
                holder.SendCommandMessages.setVisibility(CheckBox.VISIBLE);

                holder.SendChatMessages.setChecked(mBluetoothService.getChatFlag(device.getName()));
                holder.SendChatMessages.setVisibility(CheckBox.VISIBLE);
            }
            else if (mBluetoothService.getState(device) == BluetoothService.STATE_CONNECTING) {
                holder.ConnectButton.setText(mContext.getString(R.string.cancel));
                holder.SecureCheckBox.setVisibility(CheckBox.GONE);
                holder.SendCommandMessages.setVisibility(CheckBox.GONE);
                holder.SendChatMessages.setVisibility(CheckBox.GONE);
            }
        }
        else {
            holder.ConnectButton.setText(mContext.getString(R.string.connect));
            holder.SecureCheckBox.setVisibility(CheckBox.VISIBLE);
            holder.SendCommandMessages.setVisibility(CheckBox.GONE);
            holder.SendChatMessages.setVisibility(CheckBox.GONE);
        }

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return mDevices.get(position);
    }

    @Override
    public int getCount() {
        return mDevices.size();
    }

    public void add(BluetoothDevice device) {
        if (device != null) {
            if (mDeviceNames.contains(device.getName())) {
                mDevices.set(mDeviceNames.indexOf(device.getName()), device);
            }
            else {
                mDevices.add(device);
                mDeviceNames.add(device.getName());
            }
        }
    }

    public int getIndexOf (String name) {
        return mDeviceNames.indexOf(name);
    }

    public void clear() {
        mDevices.clear();
        mDeviceNames.clear();
    }

    private class ViewHolder implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{
        public TextView DeviceName;
        public Button ConnectButton;
        public CheckBox SecureCheckBox;
        public CheckBox SendChatMessages;
        public CheckBox SendCommandMessages;

        ViewHolder() {}

        @Override
        public void onClick(View v) {
            String action = ((Button)v).getText().toString();

            if (mContext.getString(R.string.connect).equals(action)) {
                mBluetoothService.connect(mDevices.get(mDeviceNames.indexOf(DeviceName.getText().toString())), SecureCheckBox.isChecked());

            }
            else {
                mBluetoothService.disconnect(mDevices.get(mDeviceNames.indexOf(DeviceName.getText().toString())));
            }
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String text = ((CheckBox)buttonView).getText().toString();
            if (text.equals(mContext.getString(R.string.send_chat_messages))) {
                mBluetoothService.setChatFlag(DeviceName.getText().toString(), isChecked);
            }
            else {
                mBluetoothService.setCommandFlag(DeviceName.getText().toString(), isChecked);
            }
        }
    }
}
