package com.videogo.ui.login;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import java.util.List;

import ezviz.ezopensdk.R;

/**
 * 服务器区域 Spinner 适配器
 * 从 MainActivity 内部类提取为独立顶层类
 */
public class ServerAreasSpAdapter implements SpinnerAdapter {

    private Context mContext;
    private List<ServerAreasEnum> mServerAreaArray;

    public ServerAreasSpAdapter(@NonNull Context context, @NonNull List<ServerAreasEnum> serverList) {
        mContext = context;
        mServerAreaArray = serverList;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getDropDownServerAreaItemView(position, convertView);
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        int cnt = 0;
        if (mServerAreaArray != null) {
            cnt = mServerAreaArray.size();
        }
        return cnt;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getServerAreaItemView(position, convertView);
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    private View getServerAreaItemView(int position, View convertView) {
        TextView itemTv = (TextView) convertView;
        if (itemTv == null) {
            itemTv = new TextView(mContext);
            itemTv.setTextColor(ContextCompat.getColor(mContext, R.color.red));
            // dp转px(applyDimension的用途是根据当前数值单位和屏幕像素密度将指定数值转换为Android标准尺寸单位px)
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, mContext.getResources().getDisplayMetrics());
            itemTv.setHeight(height);
            itemTv.setGravity(Gravity.CENTER_VERTICAL);
        }
        itemTv.setText(mServerAreaArray.get(position).areaName);
        return itemTv;
    }

    private View getDropDownServerAreaItemView(int position, View convertView) {
        TextView itemTv = (TextView) getServerAreaItemView(position, convertView);
        itemTv.setGravity(Gravity.CENTER);
        return itemTv;
    }
}
