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.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.xplora.xpsettings.Activity.WiFiActivity;
import com.xplora.xpsettings.Model.WifiModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;

import java.util.List;

public class WiFiAdapter extends ArrayAdapter<WifiModel> {

    Handler mHandler;
    Animation animation = null;
    Animation refreshAnimation = null;

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        WifiModel model = getItem(position); // 获取当前项的Fruit实例

        View view = null;
        if (model.cellType == Constant.CellType.SWITCH) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_switch, null);
            @SuppressLint("UseSwitchCompatOrMaterialCode")
            Switch itemSwitch = view.findViewById(R.id.item_switch);
            itemSwitch.setChecked(model.isOn);
            itemSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
                        Message message = new Message();
                        message.what = WiFiActivity.MSG_SWITCH_CHANGE;
                        message.obj = b ? 1 : 0;
                        mHandler.sendMessage(message);
                    }
            );
        }
        else if (model.cellType == Constant.CellType.REFRESH) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_refresh, null);
            ImageView itemIcon = view.findViewById(R.id.item_refresh);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message = new Message();
                    message.what = WiFiActivity.MSG_REFRESH_CLICK;
                    mHandler.sendMessage(message);
                }
            });
            if (refreshAnimation == null) {
                refreshAnimation = new RotateAnimation(0f, 360f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                refreshAnimation.setDuration(1000);
                refreshAnimation.setRepeatCount(-1);
                refreshAnimation.setInterpolator(new LinearInterpolator());
            }
            itemIcon.startAnimation(refreshAnimation);
        } else {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_icon_title_marquee, null);
            ImageView itemIcon = view.findViewById(R.id.item_icon);
            ImageView itemArrow = view.findViewById(R.id.item_arrow);
            itemArrow.setVisibility(model.isShowArrow ? View.VISIBLE : View.GONE);

            if(animation != null) {
                itemIcon.clearAnimation();
                animation = null;
            }
            switch (model.status) {
                case OPEN:
                    itemIcon.setImageResource(R.drawable.connections_wifi_unlocked);
                    break;
                case LOCKED:
                    itemIcon.setImageResource(R.drawable.connections_wifi_locked);
                    break;
                case CONNECTED:
                    itemIcon.setImageResource(R.drawable.connections_connected);
                    break;
                case SAVED:
                    itemIcon.setImageResource(R.drawable.connections_wifi_saved);
                    break;
                case CONNECTING:
                    itemIcon.setImageResource(R.drawable.connections_connecting);
                    if (animation == null) {
                        animation = new RotateAnimation(0f, 360f,
                                Animation.RELATIVE_TO_SELF, 0.5f,
                                Animation.RELATIVE_TO_SELF, 0.5f);
                        animation.setDuration(1000);
                        animation.setRepeatCount(-1);
                        animation.setInterpolator(new LinearInterpolator());
                    }
                    itemIcon.startAnimation(animation);
                    break;
                default:
                    //itemIcon.setImageResource(R.drawable.icon_arrow);
                    break;

            }
        }
        TextView itemTitle = view.findViewById(R.id.item_title);
        itemTitle.setText(model.title);

        return view;
    }
}
