package com.sybotan.android.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sybotan.android.demo.R;
import com.sybotan.android.demo.tools.CommonUtils;
import com.sybotan.android.demo.tools.DensityUtils;
import com.sybotan.android.demo.tools.ScreenUtils;


/**
 * function="单个itemview封装：务必指定type，样式详情看layout"
 * 0-左侧tv+右侧tv+右侧箭头
 * 1-左侧tv+右侧tv+右侧editText+箭头
 * 2-左侧tv
 * 3-左侧tv，右侧两个et
 * 4-左侧tv，右侧tv
 * Created by wangmaobo on 2018/4/24.
 */
public class ListItemView extends RelativeLayout {
    private View mContentView;
    /**
     * xml属性
     */
    private int mType;
    private String leftText;
    private String rightText, rightHint;
    private int rightTextColor;
    private OnEditTextListener mListener_et;
    /**
     * EditText设置最大输入值
     */
    private int et_max = 50;
    private ImageView mIvMust;

    public ListItemView(Context context) {
        this(context, null, -1);
    }

    public ListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (context instanceof OnEditTextListener) {
            mListener_et = (OnEditTextListener) context;
        }
        initView(context, attrs);
    }

    public ListItemView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    private void initView(Context context, AttributeSet attrs) {
        //setBackground(context.getResources().getDrawable(R.drawable.common_line_ffffff_single_all));
        if (null != attrs) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ListItemView);
            mType = array.getInt(R.styleable.ListItemView_Type, 0);
            leftText = array.getString(R.styleable.ListItemView_LeftText);
            rightText = array.getString(R.styleable.ListItemView_RightText);
            rightHint = array.getString(R.styleable.ListItemView_RightHint);
            rightTextColor = array.getInteger(R.styleable.ListItemView_RightTextColor, R.color.gray_f7f7f7);
            array.recycle();
        }
        switch (mType) {
            case 0:
            case 2:
            case 4:
            case 5:
                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item, this, true);
                TextView tv_left = mContentView.findViewById(R.id.tv_left);
                tv_left.setMaxWidth(ScreenUtils.getScreenWidth(context) / 2);
                TextView tv_right = mContentView.findViewById(R.id.tv_right);
                mIvMust = mContentView.findViewById(R.id.iv_must);
                tv_right.setHint(rightHint);
                ImageView iv_right = mContentView.findViewById(R.id.iv_right);
                if (mType == 2) {
                    iv_right.getLayoutParams().width = 0;
                }
                if (mType == 4) {
                    iv_right.setVisibility(View.INVISIBLE);
                    CommonUtils.setRightMargin(tv_right, DensityUtils.dp2px(context, 16));
                }
                if (mType == 5) {
                    TextView tv_line = mContentView.findViewById(R.id.tv_line);
                    CommonUtils.setMarginsRl(tv_line, 16, 0, 16, 0);
                }
                break;
            case 1:
                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item1, this, true);
                final EditText et = (EditText) getView(R.id.et_right);
                mIvMust = mContentView.findViewById(R.id.iv_must);
                setEditTextListener(et, et_max);
                break;
            case 3:
                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item2, this, true);
                EditText et_left = (EditText) getView(R.id.et_right1);
                EditText et_right = (EditText) getView(R.id.et_right);
                mIvMust = mContentView.findViewById(R.id.iv_must);
                setEditTextListener(et_left, 2);
                setEditTextListener(et_right, 2);
                break;
            case 6:
                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item3, this, true);
                break;
            case 7://左边有上下两行
                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item4, this, true);
                break;
            case 8://新建工单，选择建议执行人数
                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item5, this, true);
                final CommonNumberControl numberControl = (CommonNumberControl) getView(R.id.et_right);
                mIvMust = mContentView.findViewById(R.id.iv_must);
                setEditTextListener(numberControl, et_max);
                break;
//            case 9:
//                mContentView = LayoutInflater.from(context).inflate(R.layout.layout_common_list_item9, this, true);
//                final CommonNumberControl num_control = (CommonNumberControl) getView(R.id.num_control);
//                setEditTextListener(num_control.getEditText(), et_max);
//                break;
        }
        if (mType != 6 && mType != 7) {
            setTvLeft(leftText);
            setTvRight(rightText);
            setTvRightColor(rightTextColor);
        }
    }

    private void setEditTextListener(EditText et, int et_max) {
        et.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) {
                int end = et.getText().toString().trim().length();
                if (s.length() > et_max) {
                    s.delete(et_max, end);
                    et.setText(s);
                    et.setSelection(et_max);
                }
                if (null != mListener_et) {
                    mListener_et.getData(et, s.toString());
                }
            }
        });
    }

    private void setEditTextListener(CommonNumberControl et, int et_max) {
        et.getEditText().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) {
                int end = et.getEditText().getText().toString().trim().length();
                if (s.length() > et_max) {
                    s.delete(et_max, end);
                    et.getEditText().setText(s);
                    et.getEditText().setSelection(et_max);
                }
                if (null != mListener_et) {
                    mListener_et.getData(et, s.toString());
                }
            }
        });
    }

    public ListItemView setETRight1(String text) {
        if (mType == 3) {
            ((EditText) getView(R.id.et_right)).setText(text);
        }
        return this;
    }

    public ListItemView setETRight2(String text) {
        if (mType == 3) {
            EditText et = (EditText) getView(R.id.et_right1);
            et.setText(text);
            et.requestFocus();
            et.setSelection(et.getText().length());
        }
        return this;
    }

    public ListItemView setIvRight(int width) {
        ((ImageView) getView(R.id.iv_right)).getLayoutParams().width = width;
        return this;
    }

    public ListItemView setTvLeft(String text) {
        ((TextView) getView(R.id.tv_left)).setText(text);
        return this;
    }

    public View getView(int viewId) {
        return mContentView.findViewById(viewId);
    }

    public ListItemView setTvRight(String text) {
        ((TextView) getView(R.id.tv_right)).setText(text);
        return this;
    }

    public ListItemView setTvRightSize(float text) {
        ((TextView) getView(R.id.tv_right)).setTextSize(text);
        return this;
    }

    public ListItemView setTvRightColor(int color) {
        ((TextView) getView(R.id.tv_right)).setTextColor(color);
        return this;
    }

    public ListItemView setEtRight(String text) {
        if (TextUtils.isEmpty(text)) {
            return this;
        }
        if (getView(R.id.et_right) instanceof EditText) {
            ((EditText) getView(R.id.et_right)).setText(text);
            ((EditText) getView(R.id.et_right)).setSelection(((EditText) getView(R.id.et_right)).getText().length());
        } else if (getView(R.id.et_right) instanceof CommonNumberControl) {
            CommonNumberControl numberControl = (CommonNumberControl) getView(R.id.et_right);
            numberControl.setCurrentNum(text);
        }
        return this;
    }

    public ListItemView setControlRight(String text) {
        if (TextUtils.isEmpty(text)) {
            return this;
        }
        ((CommonNumberControl) getView(R.id.et_right)).setCurrentNum(text);
        ((CommonNumberControl) getView(R.id.et_right)).setSelection(((CommonNumberControl) getView(R.id.et_right)).getEditText().length());
        return this;
    }

    public ListItemView setTvRightVisible(boolean flag) {
        if (flag) {
            ((TextView) getView(R.id.tv_right)).setVisibility(VISIBLE);
        } else {
            ((TextView) getView(R.id.tv_right)).setVisibility(INVISIBLE);
        }
        return this;
    }

    public void setRightIconGone(boolean isGone) {
        if (isGone) {
            getView(R.id.iv_right).getLayoutParams().width = 0;
        }
    }

    public ListItemView setIcon(int resId) {
        ((ImageView) getView(R.id.iv_right)).setImageResource(resId);
        return this;
    }
    //=============================type1===================================

    public ListItemView setIconVisible(boolean flag) {
        if (flag) {
            ((ImageView) getView(R.id.iv_right)).setVisibility(VISIBLE);
        } else {
            ((ImageView) getView(R.id.iv_right)).setVisibility(INVISIBLE);
        }
        return this;
    }

    /* *//**
     * 将EditText设置为可编辑状态
     *
     * @return
     *//*
    public ListItemView setEditTextFocus() {
        EditText et = (EditText) getView(R.id.et_right);
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        return this;
    }*/

    /**
     * @param max
     * @return
     */
    public ListItemView setEditTextMax(int max) {
        et_max = max;
        return this;
    }

    public void setErrorCount(int errorCount) {
        if (errorCount == 0) {
            getView(R.id.error_state_linear).setVisibility(GONE);
        } else {
            getView(R.id.error_state_linear).setVisibility(VISIBLE);
        }
        TextView stateTv = (TextView) getView(R.id.error_state_tv);
        stateTv.setText("异常点" + errorCount + "个");
    }

    /**
     * 右侧箭头隐藏
     *
     * @return
     */

    public ListItemView setEditTextFocus() {
        if (getView(R.id.et_right) instanceof EditText) {
            EditText et = (EditText) getView(R.id.et_right);
            et.setFocusable(true);
            et.setFocusableInTouchMode(true);
            et.requestFocus();
        }
        return this;
    }

    /**
     * 设置白色背景
     *
     * @return
     */

//    public ListItemView setBackGroundWhite() {
//        setBackground(getResources().getDrawable(R.drawable.common_line_ffffff_single_16));
//        return this;
//    }
    public ListItemView setBackGroundGray() {
        setBackground(getResources().getDrawable(R.color.gray_F2F3F6));
        return this;
    }


    /**
     * EditText的文本监听，如果有必要则实现此接口，务必：type=1
     */
    public interface OnEditTextListener {
        void getData(View view, String data);
    }

    public void setMustVisibility(int visibility) {
        if (mIvMust != null) {
            mIvMust.setVisibility(visibility);
        }
    }
}
