package io.openim.android.ouigroup.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.TextKeyListener;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;

import io.openim.android.ouicore.base.BaseActivity;
import io.openim.android.ouicore.utils.Common;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.ouicore.vm.GroupVM;
import io.openim.android.ouicore.widget.CommonDialog;
import io.openim.android.ouigroup.databinding.ActivityGroupBulletinBinding;

@Route(path = Routes.Group.GROUP_BULLETIN)
public class GroupBulletinActivity extends BaseActivity<GroupVM, ActivityGroupBulletinBinding> {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindVMByCache(GroupVM.class);
        super.onCreate(savedInstanceState);
        bindViewDataBinding(ActivityGroupBulletinBinding.inflate(getLayoutInflater()));
        view.setGroupVM(vm);
        vm.groupId = getIntent().getStringExtra(Constants.K_GROUP_ID);
        vm.getMyMemberInfo();

        sink();
        vm.isOwnerOrAdmin.observe(this, aBoolean -> view.edit.setVisibility(aBoolean ?
                View.VISIBLE : View.GONE));

        click();
    }

    private void click() {
        view.content.setKeyListener(null);
        view.content.addTextChangedListener(textWatcher);

        view.edit.setOnClickListener(v -> {
            view.content.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.NONE, false));
            Common.pushKeyboard(this);
            v.setVisibility(View.GONE);
            view.finish.setVisibility(View.VISIBLE);
        });
        view.finish.setOnClickListener(v -> {
            Common.hideKeyboard(this, view.edit);

            CommonDialog commonDialog = new CommonDialog(GroupBulletinActivity.this);
            commonDialog.getMainView().tips.setText(getString(io.openim.android.ouicore.R.string.send_group_notice_tips));
            commonDialog.getMainView().cancel.setOnClickListener(v1 -> commonDialog.dismiss());
            commonDialog.getMainView().confirm.setOnClickListener(v1 -> {
                view.content.setKeyListener(null);
                v.setVisibility(View.GONE);
                view.edit.setVisibility(View.VISIBLE);
                commonDialog.dismiss();
                vm.UPDATEGroup(vm.groupId, null, null, vm.groupsInfo.getValue().getNotification(), null, null);
            });
            commonDialog.show();


        });
    }

    private final 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) {
            view.maxTips.setText(vm.groupsInfo.val().getNotification().length() + "/250");
        }
    };

}
