package com.example.administrator.zhapp.weight;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import com.example.administrator.zhapp.R;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by Rehome-rjb1 on 2017/5/9.
 * <p>
 * 输入框
 */

public class InputLayout extends AutoLinearLayout {

    TextView tvTitle;
    ClearEditText etContent;
    TextView tvContent;

    private View view;

    public InputLayout(Context context) {
        super(context, null);
        initView();
    }

    public InputLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
        if (attrs != null) {
            TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.InputLayout);

            String title = type.getString(R.styleable.InputLayout_tv_title);
            Boolean isShowTextView = type.getBoolean(R.styleable.InputLayout_showTextView, false);
            String hintText = type.getString(R.styleable.InputLayout_hintText);

            isShowTextView(isShowTextView);
            setTvTitle(title);
            setHintText(hintText);
            type.recycle();
        }
    }


    public InputLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {

        if (view == null) {
            view = View.inflate(getContext(), R.layout.layout_input, this);
            AutoUtils.auto(view);
            findView();
        }

        etContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });

    }

    private void findView(){
        tvTitle = view.findViewById(R.id.tv_title);
        etContent = view.findViewById(R.id.et_content);
        tvContent = view.findViewById(R.id.tv_content);
    }


    public void setTvTitle(String title) {
        tvTitle.setText(title + ":");
    }


    /**
     * 设置文本内容
     *
     * @param content
     */
    public void setContent(String content) {
        if (tvContent.getVisibility() == VISIBLE) {
            tvContent.setText(content);
        } else {
            etContent.setText(content);
        }
    }

    /**
     * 获取文本内容
     *
     * @return
     */
    public String getContent() {
        String content;
        if (tvContent.getVisibility() == VISIBLE) {
            content = tvContent.getText().toString();
        } else {
            content = etContent.getText().toString();
        }
        return content;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        etContent.setVisibility(VISIBLE);
        etContent.setEnabled(enabled);
    }

    public void isShowTextView(boolean isShow) {

        if (isShow) {
            tvContent.setVisibility(VISIBLE);
            etContent.setVisibility(GONE);
        }
    }

    public void setHintText(String hintText) {
        tvContent.setHint(hintText);
    }

    public void setTvOnClickListener(OnClickListener listener) {
        tvContent.setOnClickListener(listener);
    }
}
