package com.ewei.demo.ticket.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ewei.sdk.R;
import com.ewei.sdk.base.BaseMultiLayoutAdapter;
import com.ewei.sdk.constants.TicketValue;
import com.ewei.sdk.entity.TicketProperty;
import com.ewei.sdk.utility.Utils;


/**
 * Created by Jay on 2018/12/3.
 */
public class OpenTicketFixedFieldsAdapter extends BaseMultiLayoutAdapter<TicketProperty> {

    private final static int EM_FIELD_TEXT = 0; //跳转
    private final static int EM_FIELD_EDITTEXT = 1;//输入
    private final static int EM_FIELD_DESCRIPTION = 2;//描述

    public OpenTicketFixedFieldsAdapter(Activity context) {
        super(context);
    }

    @Override
    protected int getType(TicketProperty ticketProperty) {
        if (!TextUtils.isEmpty(ticketProperty.systemFieldKey)) {
            switch (ticketProperty.systemFieldKey) {
                case TicketValue.PROPERTIES_TYPE_DESCRIPTION:
                    return EM_FIELD_DESCRIPTION;
                case TicketValue.PROPERTIES_TYPE_SUBJECT:
                    return EM_FIELD_EDITTEXT;
                default:
                    return EM_FIELD_TEXT;
            }
        }
        return EM_FIELD_TEXT;
    }

    @Override
    protected int getLayout(int type) {
        switch (type) {
            case EM_FIELD_TEXT:
                return R.layout.listview_item_ticket_properties;
            case EM_FIELD_EDITTEXT:
                return R.layout.listview_item_ticket_editext;
            case EM_FIELD_DESCRIPTION:
                return com.ewei.demo.R.layout.ewei_open_listview_item_ticket_description;
            default:
                break;
        }
        return 0;
    }


    @Override
    protected AbstractViewHolder getViewHolder(View convertView, int type) {
        switch (type) {
            case EM_FIELD_TEXT:
                ItemTextViewHolder textViewHolder = new ItemTextViewHolder(type);
                textViewHolder.mTvName = convertView.findViewById(R.id.tv_field_name);
                textViewHolder.mTvContent = convertView.findViewById(R.id.tv_field_content);
                return textViewHolder;
            case EM_FIELD_EDITTEXT:
                ItemEditViewHolder editViewHolder = new ItemEditViewHolder(type);
                editViewHolder.mTvName = convertView.findViewById(R.id.tv_field_name);
                editViewHolder.mEtContent = convertView.findViewById(R.id.ewei_et_field_content);
                return editViewHolder;
            case EM_FIELD_DESCRIPTION:
                ItemDescriptionViewHolder descriptionViewHolder = new ItemDescriptionViewHolder(type);
                descriptionViewHolder.mTvName = convertView.findViewById(R.id.tv_field_name);
                descriptionViewHolder.mEtContent = convertView.findViewById(R.id.ewei_et_field_content);
                return descriptionViewHolder;
            default:
                break;
        }
        return null;
    }

    @Override
    protected void bindView(AbstractViewHolder viewHolder, TicketProperty ticketProperty, int type, int position) {
        if (viewHolder == null) {
            return;
        }

        if (ticketProperty.required) {
            ((BaseItemViewHolder) viewHolder).mTvName.setText(ticketProperty.name + "*");
        } else {
            ((BaseItemViewHolder) viewHolder).mTvName.setText(ticketProperty.name);
        }

        switch (type) {
            case EM_FIELD_TEXT:
                procTextInfo((ItemTextViewHolder) viewHolder, ticketProperty);
                break;
            case EM_FIELD_EDITTEXT:
                procEditInfo((ItemEditViewHolder) viewHolder, ticketProperty, position);
                break;
            case EM_FIELD_DESCRIPTION:
                procDescriptionInfo((ItemDescriptionViewHolder) viewHolder, ticketProperty, position);
                break;
            default:
                break;
        }
    }

    private void procEditInfo(final ItemEditViewHolder viewHolder, final TicketProperty ticketProperty, final int position) {
        viewHolder.mEtContent.setText(ticketProperty.content);
        viewHolder.mEtContent.setSelection(!TextUtils.isEmpty(ticketProperty.content) ? ticketProperty.content.length() : 0);
        if (!TextUtils.isEmpty(ticketProperty.notes)) {
            viewHolder.mEtContent.setHint(ticketProperty.notes);
        } else {
            viewHolder.mEtContent.setHint("");
        }
        viewHolder.mEtContent.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) {
                ticketProperty.content = viewHolder.mEtContent.getText().toString();
                setData(position, ticketProperty);
            }
        });
    }

    private void procDescriptionInfo(final ItemDescriptionViewHolder viewHolder, final TicketProperty ticketProperty, final int position) {
        viewHolder.mEtContent.setText(ticketProperty.content);
        viewHolder.mEtContent.setSelection(!TextUtils.isEmpty(ticketProperty.content) ? ticketProperty.content.length() : 0);
        if (!TextUtils.isEmpty(ticketProperty.notes)) {
            viewHolder.mEtContent.setHint(ticketProperty.notes);
        } else {
            viewHolder.mEtContent.setHint("");
        }
        viewHolder.mEtContent.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) {
                ticketProperty.content = viewHolder.mEtContent.getText().toString();
                setData(position, ticketProperty);
            }
        });
    }

    private void procTextInfo(ItemTextViewHolder ivh, TicketProperty ticketProperty) {
        ivh.mTvContent.setText(ticketProperty.content);
        if (!TextUtils.isEmpty(ticketProperty.notes)) {
            ivh.mTvContent.setHint(ticketProperty.notes);
        } else {
            ivh.mTvContent.setHint("");
        }
    }


    /**
     * 返回输入字段的值
     *
     * @param systemFieldKey
     * @return
     */
    public String getValueForKey(String systemFieldKey) {
        for (TicketProperty itemTp : getDatas()) {
            if (Utils.equals(systemFieldKey, itemTp.systemFieldKey)) {
                return itemTp.content;
            }
        }
        return "";
    }

    private abstract class BaseItemViewHolder extends AbstractViewHolder {
        TextView mTvName;

        public BaseItemViewHolder(int type) {
            super(type);
        }
    }

    private class ItemTextViewHolder extends BaseItemViewHolder {
        TextView mTvContent;

        public ItemTextViewHolder(int type) {
            super(type);
        }
    }

    private class ItemDescriptionViewHolder extends BaseItemViewHolder {
        EditText mEtContent;

        public ItemDescriptionViewHolder(int type) {
            super(type);
        }
    }

    private class ItemEditViewHolder extends BaseItemViewHolder {
        EditText mEtContent;

        public ItemEditViewHolder(int type) {
            super(type);
        }
    }
}
