package com.zztrapp.zztrapp.ui.mine.activity;

import android.view.Gravity;
import android.view.View;

import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.MassageBean;
import com.zztrapp.zztrapp.databinding.ActivityMassageDetailBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

import java.util.HashMap;

/**
 * Describe 消息详情
 * Created by Bby on 2022/8/3
 * Email bby0856@163.com
 */
public class MassageDetailActivity extends BaseActivity {

    private ActivityMassageDetailBinding binding;
    private MassageBean.RecordsBean detail;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityMassageDetailBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        detail = (MassageBean.RecordsBean) getIntent().getSerializableExtra("detail");
        binding.navView.navTitle.setText("查看");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.title.setText(detail.getTitile());
        binding.category.setText("消息类型：" + detail.getMsgCategory_dictText());
        binding.priority.setText("优先级别：" + detail.getPriority_dictText());
        binding.time.setText(detail.getSendTime());
        binding.webView.loadDataWithBaseURL(null, detail.getMsgContent(), "text/html", "utf-8", null);
        if (getIntent().getIntExtra("readFlag", 0) == 0) {
            messageRead();
        }
    }

    @Override
    protected void initOnClick() {

    }

    //查看消息
    private void messageRead() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("anntId", detail.getId());
        ISubscriberOnNextListener<HttpResult<String>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<String>>() {

            @Override
            public void onNext(HttpResult<String> httpResult) {
                if (httpResult.getCode() == 200) {

                } else {

                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<String>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("messageRead", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).messageRead(httpResultProgressSubscriber, map);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("messageRead¬");
    }
}