package com.xplora.xpsettings.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.xplora.xpsettings.Activity.TestToolsActivity;
import com.xplora.xpsettings.Activity.WiFiActivity;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

public class ConnectionsAdapter extends ArrayAdapter<BaseModel> {
    private final int resourceId;
    private Handler mHandler = null;
    List<BaseModel> mBaseObjects = new ArrayList<>();

    public ConnectionsAdapter(Context context, int textViewResourceId, List<BaseModel> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
        mBaseObjects = objects;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
        TextView itemTitle = view.findViewById(R.id.item_title);
        TextView itemDetail = view.findViewById(R.id.item_detail);
        TextView itemSwitchTV = view.findViewById(R.id.item_switchTV);
        ImageView itembackground = view.findViewById(R.id.item_background);
        ImageView itemIcon = view.findViewById(R.id.item_icon);


        BaseModel model = mBaseObjects.get(position);
        if (itemTitle != null) {
            itemTitle.setText(model.title);
        }
        if (itembackground != null) {
            itembackground.setImageResource(R.drawable.settings_bg_default);
        }

        boolean isOpen = model.isOn;
        switch (position) {
            case 0:
                if (itemIcon != null) {
                    itemIcon.setImageResource(isOpen ? R.drawable.connections_bt_1 : R.drawable.connections_bt_0);
                }
                break;
            case 1:
                if (itemIcon != null) {
                    itemIcon.setImageResource(isOpen ? R.drawable.connections_wifi_1 : R.drawable.connections_wifi_0);
                }
                break;
            case 2:
                if (itemIcon != null)
                    itemIcon.setVisibility(View.GONE);
                if (itemSwitchTV != null) {
                    itemSwitchTV.setVisibility(View.VISIBLE);
                    itemSwitchTV.setText(isOpen ? R.string.data_roaming_on : R.string.data_roaming_off);
                    itemSwitchTV.setTextColor(isOpen ? Color.GREEN : Color.RED);
                }
                break;
            default:
                break;
        }
        return view;
    }

    public void setBaseObjects(List objects) {
        mBaseObjects = objects;
    }
}
