package com.jimind.sportdemo;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jimind.sportdemo.api.ApiManager;
import com.jimind.sportdemo.base.BaseActivity;
import com.jimind.sportdemo.base.BaseObserver;
import com.jimind.sportdemo.base.ResponseResult;
import com.jimind.sportdemo.bean.Course;
import com.jimind.sportdemo.utils.AppManager;
import com.jimind.sportdemo.utils.CommonUtils;
import com.jimind.sportdemo.utils.Constant;
import com.jimind.sportdemo.utils.GlideApp;

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

/**
 * 课程详情
 *
 * @author Any
 */
public class CourseDescActivity extends BaseActivity {

    @BindView(R.id.include_title)
    FrameLayout flTitleBar;
    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;
    @BindView(R.id.tv_course_name)
    TextView tvCourseName;
    @BindView(R.id.pb_star)
    ProgressBar pbStar;
    @BindView(R.id.tv_introduce)
    TextView tvIntroduce;
    @BindView(R.id.tv_grade_class)
    TextView tvGradeClass;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.iv_video)
    ImageView ivVideo;

    private Course mCourse;
    private String mClassName;
    private int mTotalNumber;
    public static CourseDescActivity mActivity;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_course_desc;
    }

    @Override
    protected void initView() {
        CommonUtils.initTitleBar(this, flTitleBar);
        mActivity=this;
        mTotalNumber = Integer.valueOf(tvNumber.getText().toString());
    }

    @Override
    protected void initData() {
        Intent intent = this.getIntent();
        mClassName = intent.getStringExtra(Constant.CLASS_NAME);
        mCourse= (Course)intent.getSerializableExtra(Constant.COURSE);
        tvBarTitle.setText(mCourse.getCourseName());
        tvCourseName.setText(mCourse.getCourseName());
        pbStar.setProgress(mCourse.getDifficultyLevel() * CommonUtils.dip2px(this, 20));
        tvGradeClass.setText(mCourse.getGradeName() + mClassName);
        tvIntroduce.setText(mCourse.getCourseDescribe());
        GlideApp
                .with(this)
                .load(Constant.LABEL+
                        MyApplication.getInstance().getHost()
                        +Constant.RESOURCE_PORT+mCourse.getCourseDescUrl())
                .into(ivVideo);
    }

    @OnClick({R.id.ib_bar_back, R.id.bt_subtract, R.id.bt_plus, R.id.bt_start})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_bar_back:
                finish();
                break;
            case R.id.bt_subtract:
                //减少
                mTotalNumber = mTotalNumber - 5;
                tvNumber.setText(String.valueOf(mTotalNumber));
                break;
            case R.id.bt_plus:
                //增加
                mTotalNumber = mTotalNumber + 5;
                tvNumber.setText(String.valueOf(mTotalNumber));
                break;
            case R.id.bt_start:
                sendToService();
                Intent intent = new Intent(this, SignActivity.class);
                intent.putExtra(Constant.CLASS_NAME,mClassName);
                intent.putExtra(Constant.COURSE,mCourse);
                intent.putExtra(Constant.TOTAL_NUMBER,mTotalNumber);
                startActivity(intent);
                break;
            default:
        }
    }

    /**
     * 传给服务器
     */
    private void sendToService() {
        ApiManager.getInstance().requestService
                .sendCommand(Constant.SIGN, "", "")
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new BaseObserver<ResponseResult>() {
                    @Override
                    protected void onSuccess(ResponseResult responseResult) {
                    }

                    @Override
                    protected void onFailure(int code, String message) {

                    }
                });
    }
}
