package com.common.im.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.common.im.contract.NoticeEditContract;
import com.common.im.presenter.NoticeEditPresenter;
import com.common.im_ui.databinding.ActivityNoticeEditBinding;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;

import androidx.annotation.Nullable;

/**
 * 创建日期：2022/6/11 19:25
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.ChatCenter.CHAT_GROUP_NOTICE_EDIT)
public class NoticeEditActivity extends BaseMVPActivity<ActivityNoticeEditBinding, NoticeEditPresenter> implements NoticeEditContract.NoticeEditView, View.OnClickListener {
    @Override
    public void onClick(View v) {

    }

    EditText etNotice;
    EditText etTitle;
    CheckBox cbRoof;
    Button btnCommit;
    private String targetId;
    private String title;
    private String notice;
    private boolean isTop;
    private long id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "群公告编辑");
        etNotice = viewBinding.etNotice;
        etTitle = viewBinding.etTitle;
        cbRoof = viewBinding.cbRoof;
        btnCommit = viewBinding.btnCommit;


        Intent intent = getIntent();
        targetId = intent.getStringExtra("targetId");
        title = intent.getStringExtra("title");
        notice = intent.getStringExtra("notice");
        isTop = intent.getBooleanExtra("isTop", false);
        id = intent.getLongExtra("id", 0);
        cbRoof.setChecked(isTop);
        etTitle.setText(title);
        etNotice.setText(notice);
        btnCommit.setOnClickListener(v -> {
            String title = etTitle.getText().toString().trim();
            if (TextUtils.isEmpty(title)) {
                ToastUtil.getInstance().showShort("请输入标题");
                return;
            }
            String notice = etNotice.getText().toString().trim();
            if (TextUtils.isEmpty(notice)) {
                ToastUtil.getInstance().showShort("请填写群公告，1-255字");
                return;
            }
            if (TextUtils.isEmpty(NoticeEditActivity.this.title)) {
                presenter.createGroupNotice(title,notice,targetId, cbRoof.isChecked() );
            } else {
                presenter.noticeUpdate(id, targetId, title, notice, cbRoof.isChecked());
            }
        });
    }

    @Override
    protected ActivityNoticeEditBinding getLayoutView() {
        return ActivityNoticeEditBinding.inflate(getLayoutInflater());
    }

    @Override
    protected NoticeEditPresenter createPresenter() {
        return new NoticeEditPresenter();
    }

    @Override
    public void updateGroupNoticeSuccess() {
        ToastUtil.getInstance().showShort("发布成功");
        finish();
    }

    @Override
    public void createGroupNoticeSuccess() {
        ToastUtil.getInstance().showShort("发布成功");
        finish();
    }
}
