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.provider.CalendarContract;
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.Model.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;
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", "ResourceAsColor"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseModel model = mBaseObjects.get(position);

        View view = null;
        if (model.cellType == Constant.CellType.GONE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_gone, null);
        } else {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_xpstyle, null);
        }

        TextView itemTitle = view.findViewById(R.id.item_title);
        TextView itemDetail = view.findViewById(R.id.item_detail);
        ImageView itembackground = view.findViewById(R.id.item_background);
        ImageView itemIcon = view.findViewById(R.id.item_icon);


        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) {
                    if (!isOpen) {
                        itemIcon.setImageResource(R.drawable.connections_wifi_0);
                    } else {
                        itemIcon.setVisibility(model.subTitle.isEmpty() ? View.VISIBLE : View.GONE);
                        if (model.subTitle.isEmpty()) {
                            itemIcon.setImageResource(R.drawable.connections_wifi_1);
                        } else {
                            if (itemDetail != null) {
                                itemDetail.setVisibility(View.VISIBLE);
                                itemDetail.setText(model.subTitle);
                                int resId = getContext().getColor(R.color.xp_gray);
                                itemDetail.setTextColor(resId);
                            }
                        }
                    }
                }
                break;
            case 2:
            case 3:
                if (itemIcon != null)
                    itemIcon.setVisibility(View.GONE);
                if (itemDetail != null) {
                    itemDetail.setVisibility(View.VISIBLE);
                    itemDetail.setText(isOpen ? R.string.data_roaming_on : R.string.data_roaming_off);
                    itemDetail.setTextColor(isOpen ? Color.GREEN : Color.RED);
                }
                break;
            default:
                break;
        }
        return view;
    }

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