package epson.maintain;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import epson.print.R;

import java.util.ArrayList;

public class BatteryInfoAdapter extends ArrayAdapter<BatteryInfoAdapter.BatteryInfoList> {
    private LayoutInflater mInflater;
    private ArrayList<BatteryInfoList> mItems;
    private int mResourceId;

    public boolean isEnabled(int i) {
        return false;
    }

    public BatteryInfoAdapter(Context context, int i, ArrayList<BatteryInfoList> arrayList) {
        super(context, i, arrayList);
        mResourceId = i;
        mItems = arrayList;
        mInflater = (LayoutInflater) context.getSystemService("layout_inflater");
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = mInflater.inflate(mResourceId, (ViewGroup) null);
        }
        BatteryInfoList batteryInfoList = mItems.get(i);
        ((TextView) view.findViewById(R.id.bt_name)).setText(batteryInfoList.getBatteryName());
        ((TextView) view.findViewById(R.id.bt_percentage)).setText(batteryInfoList.getBatteryStatusPercentage());
        ((ImageView) view.findViewById(R.id.bt_status_icon)).setImageResource(batteryInfoList.getBatteryStatusIcon());
        return view;
    }

    static class BatteryInfoList {
        private String mBatteryName = null;
        private String mBatteryPercentage = null;
        private int mBatteryStatusIcon = 0;

        BatteryInfoList() {
        }

        private String getBatteryName() {
            return mBatteryName;
        }

        private void setBatteryStatusName(String str) {
            mBatteryName = str;
        }

        private String getBatteryStatusPercentage() {
            return mBatteryPercentage;
        }

        private void setBatteryStatusPercentage(String str) {
            mBatteryPercentage = str;
        }

        private int getBatteryStatusIcon() {
            return mBatteryStatusIcon;
        }

        private void setBatteryStatusIcon(int i) {
            mBatteryStatusIcon = i;
        }
    }

    public static class BatteryInfoManager {
        static boolean batteryIsUnknown(int i, int i2) {
            return i == 0 || i2 == 2;
        }

        public static void showBatteryList(Activity activity, int i, int i2, int[] iArr, int[] iArr2) {
            ArrayList arrayList = new ArrayList();
            for (int i3 = 0; i3 < i; i3++) {
                BatteryInfoList batteryInfoList = new BatteryInfoList();
                setBatteryName(activity, batteryInfoList, i, i3);
                setBatteryPercentage(activity, batteryInfoList, i2, iArr[i3], iArr2[i3]);
                setBatteryIcon(activity, batteryInfoList, i2, iArr[i3], iArr2[i3]);
                arrayList.add(batteryInfoList);
            }
            showListView(activity, new BatteryInfoAdapter(activity, R.layout.maintain_battery_info, arrayList));
        }

        public static void clearBatteryList(Activity activity) {
            activity.findViewById(R.id.bt_PowerText).setVisibility(View.GONE);
            showListView(activity, (BatteryInfoAdapter) null);
        }

        static void setBatteryName(Context context, BatteryInfoList batteryInfoList, int i, int i2) {
            int i3 = i2 + 1;
            if (i == 1) {
                batteryInfoList.setBatteryStatusName(context.getString(R.string.bi_remain));
                return;
            }
            batteryInfoList.setBatteryStatusName(context.getString(R.string.bi_remain) + " " + i3);
        }

        static void setBatteryPercentage(Context context, BatteryInfoList batteryInfoList, int i, int i2, int i3) {
            if (batteryIsUnknown(i, i2)) {
                return;
            }
            if (i3 < 0 || i3 > 100) {
                batteryInfoList.setBatteryStatusPercentage(context.getString(R.string.bi_no_percentage));
                return;
            }
            batteryInfoList.setBatteryStatusPercentage(i3 + " %");
        }

        static void setBatteryIcon(Activity activity, BatteryInfoList batteryInfoList, int i, int i2, int i3) {
            if (batteryIsUnknown(i, i2)) {
                batteryInfoList.setBatteryStatusIcon(R.drawable.bi_not_inserted);
                return;
            }
            batteryInfoList.setBatteryStatusIcon(activity.getResources().getIdentifier(String.format("bi_%s_%d", new Object[]{i2 == 1 ? "charge" : "running", Integer.valueOf((76 > i3 || i3 > 100) ? (51 > i3 || i3 >= 76) ? (26 > i3 || i3 >= 51) ? (i3 <= 0 || i3 >= 26) ? 0 : 39 : 59 : 89 : 100)}), "drawable", activity.getPackageName()));
        }

        public static void showPowerText(Activity activity) {
            activity.findViewById(R.id.bt_PowerText).setVisibility(View.VISIBLE);
        }

        static void showListView(Activity activity, BatteryInfoAdapter batteryInfoAdapter) {
            ListView listView = (ListView) activity.findViewById(R.id.battery_list);
            listView.setAdapter(batteryInfoAdapter);
            setListViewHeight(listView);
        }

        static void setListViewHeight(ListView listView) {
            ListAdapter adapter = listView.getAdapter();
            ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
            if (adapter == null) {
                layoutParams.height = 0;
            } else {
                int i = 0;
                for (int i2 = 0; i2 < adapter.getCount(); i2++) {
                    View view = adapter.getView(i2, (View) null, listView);
                    view.measure(0, 0);
                    i += view.getMeasuredHeight();
                }
                layoutParams.height = i + (listView.getDividerHeight() * (adapter.getCount() - 1));
            }
            listView.setLayoutParams(layoutParams);
            listView.requestLayout();
        }
    }
}
