package kr.co.hanwha.hifive.screen;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class TestActivity extends BaseActivity {
    private EditText mEditText;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_test;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return null;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mEditText = (EditText) findViewById(R.id.editText);

        mEditText.addTextChangedListener(textWatcher);
    }

    /**************************************************
     * Support methods
     **************************************************/
    private Context getContext() {
        return (Context) this;
    }

    /**************************************************
     * EditText - TextWatcher
     **************************************************/
    TextWatcher textWatcher = 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) {
            String text = s.toString();
            ArrayList<String> listOfLines = getSplitLine(text);
            if(listOfLines.size() > 1){
                for (int i = 0; i < listOfLines.size(); i++) {
                    int startPosition = text.indexOf(listOfLines.get(i));
                    int endPosition = startPosition + listOfLines.get(i).length();
                    if (i == 0) {
                        int color = HFCommonUtil.getColor(getContext(), R.color.green_13b3a9);
                        s.setSpan(new ForegroundColorSpan(color), startPosition, endPosition, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        s.setSpan(new AbsoluteSizeSpan(100), startPosition, endPosition, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }

            /*
            TextView tv = (TextView)findViewById(R.id.text);
            String string = "가나다라마바사아자차카타파하"
            int size = 50;
            SpannableStringBuilder builder = new SpannableStringBuilder(string);
            builder.setSpan(new AbsoluteSizeSpan(size), 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.append(builder);
*/

            /*
            ArrayList<String> listOfTags = getSplitStrings(text);
            for (int i = 0; i < listOfTags.size(); i++) {
                int startPosition = text.indexOf(listOfTags.get(i));
                int endPosition = startPosition + listOfTags.get(i).length();
                String word = listOfTags.get(i);
                if (word.startsWith("#")) {
                    int color = HFCommonUtil.getColor(getContext(), R.color.green_13b3a9);
                    s.setSpan(new ForegroundColorSpan(color), startPosition, endPosition, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
            */
        }
    };

    private ArrayList<String> getSplitLine(@Nullable String text) {
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.isEmpty(text)) return list;

        String[] logs = text.split("\n");
        for (int i = 0; i < logs.length; i++) {
            list.add(logs[i]);
        }

        return list;
    }

    private ArrayList<String> getSplitStrings(@Nullable String text) {
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.isEmpty(text)) return list;

        Pattern pattern = Pattern.compile("([#])([^\\s!\"#$%&'()*+,./:;<=>?@\\\\^`{|}~-]+)");
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            list.add(text.substring(matcher.start(), matcher.end()));
        }
        System.out.println(matcher.replaceAll(""));

        return list;
    }
}
