package com.sheep.gamegroup.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
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.FaqMsg;
import com.sheep.gamegroup.model.entity.MessageReplyDetailEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdpMsgFaq;
import com.sheep.gamegroup.view.adapter.AdpMsgUserFeedBack;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;

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

/**
 * created on：2018/8/28 on 17:15
 * created by: YSL
 * 描述:互动消息详情Activity------用户反馈或者faq
 */
public class MessageInteractionDetailActivity extends BaseActivity {
    @BindView(R.id.txt_baseactivity_title)
    TextView tvTitle;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title)
    TextView title;
    private RecyclerView.Adapter adapter;
    private MessageReplyDetailEntity entity;

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

    @Override
    public void initView() {
        tvTitle.setText("回复详情");
        int id = getIntent().getIntExtra("id", -1);
        loadData(id);
    }

    @OnClick({R.id.img_baseactivity_title})
    public void initClick(View view) {
        switch (view.getId()) {
            case R.id.img_baseactivity_title:
                MessageInteractionDetailActivity.this.finish();
                break;
        }
    }

    private void loadData(int id) {
        if (id <= 0) {
            return;
        }
        ViewUtil.setDefaultText(title);
        SheepApp.getInstance()
                .getNetComponent()
                .getApiService()
                .getMessageReplyItem(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        entity = baseMessage.getData(MessageReplyDetailEntity.class);
                        LinearLayoutManager manager = new LinearLayoutManager(MessageInteractionDetailActivity.this);
                        manager.setOrientation(OrientationHelper.VERTICAL);
                        recyclerView.setLayoutManager(manager);
                        if (entity.getFaq_feedback_id() > 0) {//faq
                            ViewUtil.setText(title, entity.getTitle());

                            List<FaqMsg> faqMsgList = ListUtil.emptyList();
                            FaqMsg userFaqMsg = new FaqMsg();
                            userFaqMsg.setAvatar(DataUtil.getInstance().getUserAvatar());
                            userFaqMsg.setUser(true);
                            userFaqMsg.setContent(entity.getUser_content());
                            userFaqMsg.setName("我");
                            userFaqMsg.setTime(entity.getUser_create_time());
                            faqMsgList.add(userFaqMsg);
                            FaqMsg faqMsg = new FaqMsg();
                            faqMsg.setUser(false);
                            faqMsg.setContent(entity.getContent());
                            faqMsg.setName("官方回复");
                            faqMsg.setTime(entity.getCreated_at());
                            faqMsgList.add(faqMsg);
                            adapter = new AdpMsgFaq(MessageInteractionDetailActivity.this, faqMsgList);
                        } else if (entity.getUser_feedback_id() > 0) {//用户反馈
                            ViewUtil.setText(title, entity.getUser_content());

                            adapter = new AdpMsgUserFeedBack(MessageInteractionDetailActivity.this, entity);
                        }
                        recyclerView.setAdapter(adapter);

                    }

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