package nodomain.freeyourgadget.gadgetbridge.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;

/**
 * Adapter for displaying GBDevice instances.
 */
public class GBDeviceAdapter extends ArrayAdapter<GBDevice> {

    private final Context context;

    public GBDeviceAdapter(Context context, List<GBDevice> deviceList) {
        super(context, 0, deviceList);

        this.context = context;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        GBDevice device = getItem(position);

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            view = inflater.inflate(R.layout.device_item, parent, false);
        }
        TextView deviceStatusLabel = (TextView) view.findViewById(R.id.device_status);
        TextView deviceNameLabel = (TextView) view.findViewById(R.id.device_name);
        TextView deviceInfoLabel = (TextView) view.findViewById(R.id.device_info);
        TextView batteryStatusLabel = (TextView) view.findViewById(R.id.battery_status);
        ImageView deviceImageView = (ImageView) view.findViewById(R.id.device_image);
        ProgressBar busyIndicator = (ProgressBar) view.findViewById(R.id.device_busy_indicator);

        deviceNameLabel.setText(getUniqueDeviceName(device));
        deviceInfoLabel.setText(device.getInfoString());

        if (device.isBusy()) {
            deviceStatusLabel.setText(device.getBusyTask());
            busyIndicator.setVisibility(View.VISIBLE);
            batteryStatusLabel.setVisibility(View.GONE);
            deviceInfoLabel.setVisibility(View.GONE);
        } else {
            deviceStatusLabel.setText(device.getStateString());
            busyIndicator.setVisibility(View.GONE);
            batteryStatusLabel.setVisibility(View.VISIBLE);
            deviceInfoLabel.setVisibility(View.VISIBLE);
        }

        short batteryLevel = device.getBatteryLevel();
        if (batteryLevel != GBDevice.BATTERY_UNKNOWN) {
            batteryStatusLabel.setText("BAT: " + device.getBatteryLevel() + "%");
            BatteryState batteryState = device.getBatteryState();
            if (BatteryState.BATTERY_LOW.equals(batteryState)) {
                batteryStatusLabel.setTextColor(Color.RED);
            } else {
                batteryStatusLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.secondarytext));

                if (BatteryState.BATTERY_CHARGING.equals(batteryState) ||
                        BatteryState.BATTERY_CHARGING_FULL.equals(batteryState)) {
                    batteryStatusLabel.append(" CHG");
                }
            }
        } else {
            batteryStatusLabel.setText("");
        }

        switch (device.getType()) {
            case PEBBLE:
                deviceImageView.setImageResource(R.drawable.ic_device_pebble);
                break;
            case MIBAND:
                deviceImageView.setImageResource(R.drawable.ic_device_miband);
                break;
            default:
                deviceImageView.setImageResource(R.drawable.ic_launcher);
        }

        return view;
    }

    private String getUniqueDeviceName(GBDevice device) {
        String deviceName = device.getName();
        if (!isUniqueDeviceName(device, deviceName)) {
            deviceName = deviceName + " " + device.getShortAddress();
        }
        return deviceName;
    }

    private boolean isUniqueDeviceName(GBDevice device, String deviceName) {
        for (int i = 0; i < getCount(); i++) {
            GBDevice item = getItem(i);
            if (item == device) {
                continue;
            }
            if (deviceName.equals(item.getName())) {
                return false;
            }
        }
        return true;
    }
}

@@ -1460,24 +1460,25 @@
 w deviceInfo
+1
 Label = (Tex
@@ -1518,16 +1518,102 @@
 ice_info
+1);%0A        TextView deviceInfo2Label = (TextView) view.findViewById(R.id.device_info2
 );%0A     
@@ -1952,32 +1952,93 @@
       deviceInfo
+1Label.setText(device.getHWInfoString());%0A        deviceInfo2
 Label.setText(de
@@ -2279,32 +2279,88 @@
       deviceInfo
+1Label.setVisibility(View.GONE);%0A            deviceInfo2
 Label.setVisibil
@@ -2560,32 +2560,32 @@
 (View.VISIBLE);%0A
-
             devi
@@ -2590,16 +2590,75 @@
 viceInfo
+1Label.setVisibility(View.VISIBLE);%0A            deviceInfo2
 Label.se
@@ -4149,24 +4149,160 @@
 Name + %22 %22 +
+ device.getHardwareVersion();%0A            if (!isUniqueDeviceName(device, deviceName)) %7B%0A                deviceName = deviceName + %22 %22 +
  device.getS
@@ -4312,24 +4312,38 @@
 tAddress();%0A
+            %7D%0A
         %7D%0A  

Use hw version to make device names unique, then mac addr