package com.sheep.gamegroup.view.activity;

import android.content.Intent;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.SystemNotification;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * created on：2018/9/3 on 20:50
 * created by: YSL
 * 描述:
 */
public class ActSysNfDetail extends BaseActivity {

    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @Override
    protected boolean needButterKnife() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_sys_nf_detail;
    }

    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setTitle(this,"消息详情")
                .setShowOrHide(this, true)
                .setTitleFinish(this);
    }

    @Override
    public void initListener() {
        tvTitle.setMovementMethod(new ScrollingMovementMethod());
    }
    private void loadData(SystemNotification systemNotification) {
        if (systemNotification != null) {
            ViewUtil.setText(tvContent, systemNotification.getContent());
            ViewUtil.setText(tvTitle, systemNotification.getTitle());
        }
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        if (id == -1) {
            SystemNotification systemNotification = (SystemNotification) intent.getSerializableExtra(SystemNotification.class.getSimpleName());
            loadData(systemNotification);
        } else {
            initSysNf(id);
        }
    }
    private void initSysNf(int id) {
        if (id <= 0) {
            G.showToast("消息id出错");
            return;
        }
        ViewUtil.setDefaultText(tvContent);
        ViewUtil.setDefaultText(tvTitle);
        SheepApp.getInstance()
                .getNetComponent()
                .getApiService()
                .getSystemNotification(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        SystemNotification entity = baseMessage.getData(SystemNotification.class);
                        loadData(entity);
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast(baseMessage);
                    }
                });
    }
}
