package com.epson.mobilephone.common.ble;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epson.mobilephone.common.ble.util.ScannedDevice;

import epson.print.IprintApplication;

import java.util.Iterator;
import java.util.List;

public class DeviceAdapter extends ArrayAdapter<ScannedDevice> {
    private static final String PREFIX_RSSI = "RSSI:";
    private LayoutInflater mInflater;
    private List<ScannedDevice> mList;
    private int mResId;

    public DeviceAdapter(Context context, int i, List<ScannedDevice> list) {
        super(context, i, list);
        mResId = i;
        mList = list;
        mInflater = (LayoutInflater) context.getSystemService("layout_inflater");
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        ScannedDevice scannedDevice = (ScannedDevice) getItem(i);
        if (view == null) {
            view = mInflater.inflate(mResId, (ViewGroup) null);
        }
        TextView textView = (TextView) view.findViewById(R.id.device_name);
        textView.setText(scannedDevice.getDisplayName());
        if (IprintApplication.isReleaseUnlimited()) {
            ((TextView) view.findViewById(R.id.device_address)).setText(scannedDevice.getDevice().getAddress());
            TextView textView2 = (TextView) view.findViewById(R.id.device_rssi);
            textView2.setText(PREFIX_RSSI + Integer.toString(scannedDevice.getRssi()));
            textView2.setTextColor(-3355444);
        } else {
            view.findViewById(R.id.device_address).setVisibility(View.GONE);
            view.findViewById(R.id.device_rssi).setVisibility(View.GONE);
            textView.setPadding(0, 20, 0, 20);
        }
        ((ImageView) view.findViewById(R.id.signal)).setImageDrawable(getThresholdIcon(scannedDevice.getRssi()));
        return view;
    }

    private Drawable getThresholdIcon(int i) {
        Resources resources = getContext().getResources();
        int i2 = R.drawable.signal_1;
        if (i >= -60) {
            EpLog.m77i("HIGH_SIGNAL");
            i2 = R.drawable.signal_3;
        } else if (i >= -70) {
            EpLog.m77i("MID_SIGNAL");
            i2 = R.drawable.signal_2;
        } else if (i >= -75) {
            EpLog.m77i("LOW_SIGNAL");
        }
        return resources.getDrawable(i2);
    }

    public void update(BluetoothDevice bluetoothDevice, int i, String str, byte[] bArr) {
        if (bluetoothDevice != null && bluetoothDevice.getAddress() != null) {
            boolean z = false;
            Iterator<ScannedDevice> it = mList.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                ScannedDevice next = it.next();
                if (bluetoothDevice.getAddress().equals(next.getDevice().getAddress())) {
                    z = true;
                    next.setRssi(i);
                    break;
                }
            }
            if (!z) {
                mList.add(new ScannedDevice(bluetoothDevice, i, str, bArr));
            }
            notifyDataSetChanged();
        }
    }
}
