package com.sheep.gamegroup.view.fragment;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lqr.emoji.EmotionKeyboard;
import com.lqr.emoji.EmotionLayout;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.DiscoveryVideo;
import com.sheep.gamegroup.model.entity.UserComment;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ApiJSONUtil;
import com.sheep.gamegroup.util.KeyEventUtil;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdpArticleComment;
import com.sheep.gamegroup.view.dialog.DialogSuccessUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * Created by realicing on 2018/11/2.
 * realicing@sina.com
 * 小绵羊3.4.5新增 -- 视频详情--用户评论列表界面
 */
public class FgtVideoComment extends BaseListFragment4<UserComment> {

    @Override
    public int getLayoutId() {
        return R.layout.fgt_video_comment;
    }

    @BindView(R.id.rlContent)
    RelativeLayout rlContent;
    @BindView(R.id.elEmotion)
    EmotionLayout elEmotion;
    @BindView(R.id.video_comment_title_tv)
    TextView video_comment_title_tv;
    @BindView(R.id.video_comment_rv)
    RecyclerView video_comment_rv;
    //输入框
    @BindView(R.id.input_comment_input)
    EditText input_comment_input;
    @BindView(R.id.input_comment_exp)
    ImageView input_comment_exp;

    //表情键盘协调工具
    private EmotionKeyboard mEmotionKeyboard;

    @Override
    public void initView() {
        view_list = video_comment_rv;
        initAdapter();
        ViewUtil.setText(video_comment_title_tv, getString(R.string.total_x_comment, ViewUtil.BLOCK));

        //初始化 表情键盘协调工具
        initEmotionKeyboard();
        initSticker();
        refreshData();
    }

    //用户评论一个用户对游戏的评论
    private void commitContent(String content) {
        if (TestUtil.isDev()) G.showToast("评论内容：" + content);
        if (content.isEmpty()) {
            G.showToast("评论内容不能为空");
            return;
        }
        // 发表评论
        ApiJSONUtil.postGameUserUserGameComment(type, video_id, content, 0)
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        input_comment_input.setText("");
                        DialogSuccessUtil.showSuccess("发表成功", new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialogInterface) {
                                refreshData();
                            }
                        });
                    }

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


    private int video_id;
    private int type = ApiJSONUtil.COMMENT_TYPE_VIDEO;

    public void setVideo_id(int video_id) {
        this.video_id = video_id;
    }


    private void initAdapter() {
        video_comment_rv.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        //用户评论的回复列表
        adapter = new AdpArticleComment(R.layout.item_video_comment, list).setHasCommentDetail(false);
        adapter.bindToRecyclerView(video_comment_rv);
        adapter.setEmptyView(R.layout.include_empty);
    }

    @Override
    protected String getKey(int page, int per_page) {
        return String.format(Locale.CHINA, "app/game_user/app_comment_list?page_no=%d&page_size=%d&app_id=%d&comment_type=%d", page, per_page, video_id, type);
    }

    @Override
    protected Observable<BaseMessage> getApi(ApiService apiService) {
        return apiService.getGameUserAppCommentList(page, per_page, video_id, type);
    }

    @Override
    protected Class<UserComment> getTClass() {
        return UserComment.class;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        if(lastMessage != null)
            ViewUtil.setText(video_comment_title_tv, R.string.total_x_comment, String.valueOf(lastMessage.getTotal()));
    }

    private void initSticker() {
        elEmotion.attachEditText(input_comment_input);
    }

    //初始化 表情键盘协调工具
    private void initEmotionKeyboard() {
        mEmotionKeyboard = EmotionKeyboard.with(activity);
        mEmotionKeyboard.bindToContent(rlContent);
        mEmotionKeyboard.bindToEmotionButton(input_comment_exp);
        mEmotionKeyboard.bindToEditText(input_comment_input);
        mEmotionKeyboard.setEmotionLayout(elEmotion);
    }

    //返回处理
    public boolean onBackPressed() {
        if (mEmotionKeyboard != null) {
            return mEmotionKeyboard.interceptBackPress(true);
        }
        return false;
    }

    public void loadVideoData(DiscoveryVideo video) {
        if (video == null) {
            ViewUtil.setText(video_comment_title_tv, R.string.total_x_comment, ViewUtil.BLOCK);
            return;
        }
        ViewUtil.setText(video_comment_title_tv, R.string.total_x_comment, String.valueOf(video.getComment()));
    }

    //点击评论界面中的x
    @OnClick({R.id.video_comment_x})
    public void onClickX(View view) {
        KeyEventUtil.sendKeyDownUp(KeyEvent.KEYCODE_BACK);
    }

    //点击评论界面中的评论按钮
    @OnClick({R.id.input_comment_commit})
    public void commitVideoComment(View view) {
        String content = input_comment_input.getText().toString();
        commitContent(content);
    }
}
