package com.xplora.xpsettings.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
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.IconProgressView;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.Model.IconProgressModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.KeyUtils;
import com.xplora.xpsettings.Utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

public class BaseListAdapter extends ArrayAdapter<BaseModel> {
    private Handler mHandler = null;
    List<BaseModel> mBaseObjects = new ArrayList<>();
    private KeyUtils mKeyUtils = null;

    public BaseListAdapter(Context context, int textViewResourceId, List objects) {
        super(context, textViewResourceId, objects);
        mBaseObjects = objects;
    }

    public BaseListAdapter(Context context, int textViewResourceId, List objects, Handler handler) {
        super(context, textViewResourceId, objects);
        mBaseObjects = objects;
        mHandler = handler;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseModel model = mBaseObjects.get(position);

        View view = null;
        if (model.cellType == Constant.CellType.TITLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_title, null);
        } else if (model.cellType == Constant.CellType.TITLE_SUBTITLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_title_subtitle, null);
        } else if (model.cellType == Constant.CellType.TITLE_CENTER) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_title_center, null);
        } else if (model.cellType == Constant.CellType.TITLE_EDIT) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_title_edit, null);
        } else if (model.cellType == Constant.CellType.ICON_TITLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_icon_title, null);
        } else if (model.cellType == Constant.CellType.ICON_TITLE_SUBTITLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_icon_title_subtitle, null);
        }else if (model.cellType == Constant.CellType.PROGRESS_TITLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_progress_title, null);
        }else if (model.cellType == Constant.CellType.PROGRESS_TITLE_SUBTITLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_progress_title_subtitle, null);
        } else if (model.cellType == Constant.CellType.XPSTYLE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_xpstyle, null);
        } else if (model.cellType == Constant.CellType.SWITCH) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_switch, null);
        } else if (model.cellType == Constant.CellType.CHECKBOX) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_checkbox, null);
        } else if (model.cellType == Constant.CellType.REFRESH) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_refresh, null);
        } else if (model.cellType == Constant.CellType.CANCEL_SURE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.view_cancel_sure, null);
        }else if (model.cellType == Constant.CellType.SURE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.view_sure, null);
        } else if (model.cellType == Constant.CellType.MESSAGE) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.view_message_text, null);
        } else if (model.cellType == Constant.CellType.EMPTY) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.view_empty, null);
        }

        assert view != null;
        TextView itemTitle = view.findViewById(R.id.item_title);
        TextView itemSubTitle = view.findViewById(R.id.item_subtitle);
        EditText itemEdit = view.findViewById(R.id.item_edit);
        ImageView itembackground = view.findViewById(R.id.item_background);
        ImageView itemIcon = view.findViewById(R.id.item_icon);
        ImageView itemArrow = view.findViewById(R.id.item_arrow);
        IconProgressView iconProgressView = view.findViewById(R.id.item_icon_progress);
        Switch switchButton = view.findViewById(R.id.item_switch);
        CheckBox checkboxButton = view.findViewById(R.id.item_checkbox);
        Button cancelButton = view.findViewById(R.id.button_cancel);
        Button sureButton = view.findViewById(R.id.button_sure);


        if (itemTitle != null) {
            itemTitle.setText(model.title);
        }
        if (itemSubTitle != null) {
            itemSubTitle.setText(model.subTitle);
            if (model.subColor != 0) {
                itemSubTitle.setTextColor(getContext().getColor(model.subColor));
            }
        }
        if (itembackground != null) {
            itembackground.setImageResource(model.background);
        }
        if (itemIcon != null) {
            itemIcon.setImageResource(model.icon);
        }
        if (itemArrow != null) {
            itemArrow.setVisibility(model.isShowArrow ? View.VISIBLE : View.GONE);
        }
        if (switchButton != null) {
            switchButton.setChecked(model.isOn);
            switchButton.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (mHandler == null)
                        return;
                    Message message = new Message();
                    message.what = Constant.MSG_SWITCHC_CHANGE;
                    message.arg1 = position;
                    message.arg2 = b ? 1 : 0;
                    mHandler.sendMessage(message);
                }
            );
        }
        if (checkboxButton != null) {
            checkboxButton.setChecked(model.isOn);
            checkboxButton.setEnabled(false);
            checkboxButton.setFocusable(false);
            checkboxButton.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (mHandler == null)
                        return;
                    Message message = new Message();
                    message.what = Constant.MSG_CHECKBOX_CHANGE;
                    message.arg1 = position;
                    mHandler.sendMessage(message);
                }
            );
        }
        if (iconProgressView != null) {
            iconProgressView.setIcon(model.icon);
            //iconProgressView.setProgressColor(((IconProgressModel)model).progressColor);
            iconProgressView.setProgress100(((IconProgressModel)model).progress);
        }
        if (cancelButton != null) {
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mHandler == null)
                        return;
                    Message message = new Message();
                    message.what = Constant.MSG_CANCEL_CLICK;
                    mHandler.sendMessage(message);
                }
            });
        }
        if (sureButton != null) {
            sureButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mHandler == null)
                        return;
                    Message message = new Message();
                    message.what = Constant.MSG_SURE_CLICK;
                    mHandler.sendMessage(message);
                }
            });
        }

        if (itemEdit != null) {
            itemEdit.setText(model.value);
            itemEdit.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (mHandler == null)
                        return;
                    Message message = new Message();
                    message.what = Constant.MSG_TEXTEDIT_FINISH;
                    message.arg1 = position;
                    message.obj = s.toString();
                    mHandler.sendMessage(message);
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mKeyUtils = new KeyUtils(getContext(), itemEdit);
                }
            });
        }

        return view;
    }

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

    public void closeKeyboard() {
        if (mKeyUtils != null) {
            mKeyUtils.closeKeyboard();
        }
    }

    public boolean isKeyboardVisible() {
        if (mKeyUtils != null) {
            return mKeyUtils.isKeyboardVisible();
        }
        return false;
    }
}
