package com.xplora.xpsettings.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
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 java.util.List;

public class ListAdapter extends ArrayAdapter<BaseModel> {
    private final int resourceId;
    private int selectedItem = -1;
    private Handler mHandler = null;

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

    public ListAdapter(Context context, int textViewResourceId, List<BaseModel> objects, Handler handler) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
        mHandler = handler;
    }


    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseModel model = getItem(position); // 获取当前项的Fruit实例
        View view;
        if (mHandler != null && model.title.equals("loggable")) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_switch, null);
            Switch itemSwitch = view.findViewById(R.id.item_switch);
            itemSwitch.setChecked(model.isOn);
            itemSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
                        Message message = new Message();
                        message.what = TestToolsActivity.MSG_LOGGABLE_CHANGE;
                        message.obj = b ? 1 : 0;
                        mHandler.sendMessage(message);
                    }
            );
        } else if (mHandler != null && model.title.equals("Debugable")) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_switch, null);
            Switch itemSwitch = view.findViewById(R.id.item_switch);
            itemSwitch.setChecked(model.isOn);
            itemSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
                        Message message = new Message();
                        message.what = TestToolsActivity.MSG_DEBUGABLE_CHANGE;
                        message.obj = b;
                        mHandler.sendMessage(message);
                    }
            );
        } else {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
        }

        TextView itemTitle = view.findViewById(R.id.item_title);
        TextView itemDetailTitle = view.findViewById(R.id.item_detail);
        ImageView itemIcon = view.findViewById(R.id.item_icon);
        ImageView itemArrow = view.findViewById(R.id.item_arrow);

        if (itemTitle != null) {
            itemTitle.setText(model.title);
            if (position == selectedItem)
                itemTitle.setTextColor(getContext().getColor(R.color.blue));
        }
        if (itemDetailTitle != null)
            itemDetailTitle.setText(model.value);
        if (itemIcon != null)
            itemIcon.setImageResource(model.icon);
        if (itemArrow != null)
            itemArrow.setVisibility(model.isShowArrow ? View.VISIBLE : View.GONE);

        return view;
    }

    public void setItemSelected(int item) {
        selectedItem = item;
    }
}
