package com.usai.redant.CommonEditor.CommonEditor;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.usai.redant.CommonEditor.CommonEditorActivity;
import com.usai.redant.redantmobile.R;

import org.json.JSONObject;

/**
 * Created by macmini1 on 2017/12/13.
 */

public class CommonEditorRangeCellHolder extends CommonEditorCellHolder {

    public TextView title_lb;
    public EditText min_et;
    public EditText max_et;
    public Button reset_btn;

    public TextWatcher minTextChangeListener;

    public TextWatcher maxTextChangeListener;

    boolean processValues(String min_str, String max_str) {
        float min = 0;
        float max = Float.MAX_VALUE;

        if (min_str != null && !min_str.isEmpty()) {
            min = Float.valueOf(min_str);
        }
        if (max_str != null && !max_str.isEmpty()) {
            max = Float.valueOf(max_str);
        }

        if (max < min) {

            return false;
        }
        ((CommonEditorActivity)mCtx).commonEditorRangeDidChange(min_str,max_str,group,child);

        return true;
    }

    public void clearTextWather() {
        min_et.removeTextChangedListener(minTextChangeListener);
        minTextChangeListener = null;

        max_et.removeTextChangedListener(maxTextChangeListener);
        maxTextChangeListener = null;
    }

    public void resetTextWather() {

        minTextChangeListener = new TextWatcher() {

            String origin_text;
            String string;
            int start_position;
            int change_length; // 修改原文长度
            int new_string_length;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                origin_text = s.toString();
                start_position = start;
                change_length = count;
                new_string_length = after;

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                /**
                 * 文本处理
                 * */
                if (new_string_length == 0) {
                    string = "";
                } else {
                    string = s.toString().substring(start_position,start_position + new_string_length);
                }

                if (change_length == 0) { // 新加内容

                } else { // 替换原文或删除原文

                }

                /**
                 * 业务逻辑
                 */
                String max_str = max_et.getText().toString();
                String min_str = min_et.getText().toString();

                if (!processValues(min_str,max_str)) {
                    min_et.setText(origin_text);
                }

            }
        };

        maxTextChangeListener  = new TextWatcher() {

            String origin_text;
            String string;
            int start_position;
            int change_length; // 修改原文长度
            int new_string_length;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                origin_text = s.toString();
                start_position = start;
                change_length = count;
                new_string_length = after;

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                /**
                 * 文本处理
                 * */
                if (new_string_length == 0) {
                    string = "";
                } else {
                    string = s.toString().substring(start_position,start_position + new_string_length);
                }

                if (change_length == 0) { // 新加内容

                } else { // 替换原文或删除原文

                }

                /**
                 * 业务逻辑
                 */
                String max_str = max_et.getText().toString();
                String min_str = min_et.getText().toString();

                if (!processValues(min_str,max_str)) {
                    min_et.setText(origin_text);
                }

            }
        };

    }

    private class ResetClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            ((CommonEditorActivity)mCtx).commonEditorRangeDidChange("","",group,child);
        }
    }


    public CommonEditorRangeCellHolder(Context context, View view) {
        super(context, view);

        title_lb = view.findViewById(R.id.range_title_tv);
        min_et = view.findViewById(R.id.range_minimum_et);
        max_et = view.findViewById(R.id.range_maximum_et);
        reset_btn = view.findViewById(R.id.range_reset_btn);

        reset_btn.setOnClickListener(new ResetClickListener());
    }

    @Override
    public void setItem(JSONObject item_json, boolean readonly, View view) {
        super.setItem(item_json, readonly, view);
    }
}
