package io.openim.android.ouicore.widget;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.StringUtils;

import java.io.Serializable;

import io.openim.android.ouicore.R;
import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.base.BaseViewModel;
import io.openim.android.ouicore.databinding.ActivitySingleModifyBinding;
import io.openim.android.ouicore.utils.Common;

public class SingleInfoModifyActivity extends BaseActivity<BaseViewModel, ActivitySingleModifyBinding> {
    public static final String SINGLE_INFO_MODIFY_DATA = "single_info_modify_data";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindViewDataBinding(ActivitySingleModifyBinding.inflate(getLayoutInflater()));
        sink();
        SingleInfoModifyData singleInfoModifyData = (SingleInfoModifyData) getIntent().getSerializableExtra(SINGLE_INFO_MODIFY_DATA);
        view.title.setText(singleInfoModifyData.title);
        view.description.setText(singleInfoModifyData.description);
        view.avatar.load(singleInfoModifyData.avatarUrl, StringUtils.isEmpty(singleInfoModifyData.type));
        view.editText.setText(singleInfoModifyData.editT);
        canSubmit(!StringUtils.isEmpty(singleInfoModifyData.editT));
        view.editText.post(() -> {
            view.editText.requestFocus();
            view.editText.setSelection(view.editText.getText().length());
        });
        view.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

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

            }

            @Override
            public void afterTextChanged(Editable editable) {
                canSubmit(!StringUtils.isEmpty(editable.toString()));
            }
        });
    }

    private void canSubmit(boolean can) {
        view.submit.setBackgroundResource(can ? R.drawable.sty_radius_4_02c25f : R.drawable.sty_radius_4_f2f2f2);
        view.submit.setTextColor(can ? Color.WHITE : Color.parseColor("#BFBFBF"));
        view.del.setVisibility(can ? View.VISIBLE : View.GONE);
    }

    public void complete(View v) {
        String input = view.editText.getText().toString();
        if (Common.isBlank(input)) {
            toast(getString(R.string.empty_prompt));
            return;
        }
        setResult(RESULT_OK, new Intent().putExtra(SINGLE_INFO_MODIFY_DATA, input));
        finish();
    }
    public void delete(View v) {
        view.editText.setText("");
    }

    public static class SingleInfoModifyData implements Serializable {
        public String title;
        public String description;
        public String avatarUrl;
        public String editT;
        public String type = "";
    }
}
