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.ImageView;
import android.widget.Switch;
import android.widget.TextView;

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

import java.util.List;

public class BluetoothDevicesAdapter extends ArrayAdapter<BluetoothModel> {
    public Context mContext = null;
    public Handler mHandler;
    public Animation animation = null;
    public Animation refreshAnimation = null;
    public ImageView itemRefreshIcon = null;
    public boolean mIsScan = false;

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BluetoothModel 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 = Constant.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);
            itemRefreshIcon = view.findViewById(R.id.item_refresh);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message = new Message();
                    message.what = Constant.MSG_REFRESH_CLICK;
                    mHandler.sendMessage(message);
                }
            });
            if (mIsScan) {
                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());
                }
                itemRefreshIcon.startAnimation(refreshAnimation);
            } else {
                if (refreshAnimation != null) {
                    refreshAnimation.cancel();
                    refreshAnimation = null;
                }
                if (itemRefreshIcon != null) {
                    itemRefreshIcon.clearAnimation();
                }
            }
        } else {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_icon_title_subtitle_marquee, null);
            ImageView itemArrow = view.findViewById(R.id.item_arrow);
            itemArrow.setVisibility( View.VISIBLE);

            ImageView itemIcon = view.findViewById(R.id.item_icon);
            TextView itemSubTitle = view.findViewById(R.id.item_subtitle);

            if(animation != null) {
                itemArrow.clearAnimation();
                animation = null;
            }
            switch (model.status) {
                case CONNECTED:
                    itemIcon.setImageResource(R.drawable.connections_connected);
                    itemSubTitle.setText(mContext.getString(R.string.bt_connected));
                    break;
                case PAIRED:
                    itemIcon.setImageResource(R.drawable.connections_bt_paired);
                    itemSubTitle.setText(mContext.getString(R.string.bt_paired));
                    break;
                case PAIRING:
                    itemIcon.setImageResource(R.drawable.connections_connecting);
                    itemSubTitle.setText(mContext.getString(R.string.bt_pairing));
                    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.connections_bt_unpaired);
                    itemSubTitle.setText(mContext.getString(R.string.bt_notpaired));
                    break;
            }
        }
        TextView itemTitle = view.findViewById(R.id.item_title);
        itemTitle.setText(model.title);
        return view;
    }

    public void setIsScan(boolean isScan) {
        mIsScan = isScan;

//        if (isScan) {
//            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());
//            }
//            itemRefreshIcon.startAnimation(refreshAnimation);
//        } else {
//            if (refreshAnimation != null) {
//                refreshAnimation.cancel();
//                refreshAnimation = null;
//            }
//            if (itemRefreshIcon != null) {
//                itemRefreshIcon.clearAnimation();
//            }
//        }
    }
}
