package com.sheep.gamegroup.view.activity;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ApiJSONUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.ViewUtil;
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 com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/11/1.
 * realicing@sina.com
 * 评论游戏
 */
public class ActCommentGameApp extends BaseActivity {
    @BindView(R.id.act_comment_game_app_score)
    RatingBar act_comment_game_app_score;
    @BindView(R.id.act_comment_game_app_input_comment)
    EditText act_comment_game_app_input_comment;

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

    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "写评价")
                .setEndTv(this, "发表", 0xFF2EBEF2, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int progress = act_comment_game_app_score.getProgress();
                        String content = act_comment_game_app_input_comment.getText().toString();
                        commitUserGameComment(progress, content);
                    }
                })
                .setTitleFinish(this);
//
//        Observable.just(1).delay(100L, TimeUnit.MILLISECONDS)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<Integer>() {
//                    @Override
//                    public void call(Integer integer) {
//                        act_comment_game_app_input_comment.setFocusable(true);
//                        act_comment_game_app_input_comment.setFocusableInTouchMode(true);
//                        act_comment_game_app_input_comment.requestFocus();
//                    }
//                });
    }

    //评论游戏
    private void commitUserGameComment(int score, String content) {
        if (TestUtil.isDev()) G.showToast(score + " " + content);
        if (TextUtils.isEmpty(content)) {
            G.showToast("亲，您还没有填写你的观点呢");
            return;
        }
        if(score < 1){
            G.showToast("亲，请先打分");
            return;
        }
        ApiJSONUtil.postGameUserUserGameComment(ApiJSONUtil.COMMENT_TYPE_GAME, game_id, content, score)
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        act_comment_game_app_input_comment.setText("");
                        DialogSuccessUtil.showSuccess("发表成功", new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialogInterface) {
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                    }

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


    private int game_id;

    @Override
    public void initData() {
        game_id = getIntent().getIntExtra("game_id", 0);
        if (game_id > 0) {
//            if (TestUtil.isDev()) G.showToast("game_id为" + game_id);
            act_comment_game_app_score.setIsIndicator(false);
        } else {
            G.showToast(R.string.error_data);
            finish();
        }
    }

    //点击x来重置评分
    public void onClickXScore(View view) {
        act_comment_game_app_score.setRating(0);
    }

    //点击x来清空输入内容
    public void onClickXInput(View view) {
        ViewUtil.setText(act_comment_game_app_input_comment);
    }
}
