package com.jimind.sportdemo;

import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
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.CommonUtils;
import com.jimind.sportdemo.utils.Constant;
import com.jimind.sportdemo.view.SignProgressBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 签到
 *
 * @author Any
 */
public class SignActivity extends BaseActivity {

    @BindView(R.id.fl_title_bar)
    FrameLayout flTitleBar;
    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;
    @BindView(R.id.tv_sign_course)
    TextView tvSignCourse;
    @BindView(R.id.tv_sign_class)
    TextView tvSignClass;
    @BindView(R.id.tv_total_count)
    TextView tvTotalCount;
    @BindView(R.id.tv_minute)
    TextView tvMinute;
    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.pb_sign)
    SignProgressBar pbSign;

    private Course mCourse;
    private String mClassName;
    private Disposable mDisposable;
    private int mCurrentMills, mCurrentProgress, mTotalNumber;

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

    @Override
    protected void initView() {
        CommonUtils.initTitleBar(this, flTitleBar);
        tvBarTitle.setText("正在签入中");
        Intent intent = this.getIntent();
        mClassName = intent.getStringExtra(Constant.CLASS_NAME);
        mTotalNumber = intent.getIntExtra(Constant.TOTAL_NUMBER, 0);
        mCourse = (Course) intent.getSerializableExtra(Constant.COURSE);

        tvSignCourse.setText(mCourse.getCourseName());
        tvSignClass.setText(mCourse.getGradeName() + mClassName);
        tvTotalCount.setText("应到：" + mTotalNumber + "人");
        pbSign.setTotalProgress(mTotalNumber);
    }

    @Override
    protected void initData() {

        Observable.interval(0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        mCurrentMills++;
                        int minute = mCurrentMills / 60;
                        int second = mCurrentMills % 60;
                        if (second < 10) {
                            tvSecond.setText("0" + second + "秒");
                        } else {
                            tvSecond.setText(second + "秒");
                        }

                        if (minute < 10) {
                            tvMinute.setText("0" + minute + "分钟");
                        } else {
                            tvMinute.setText(minute + "分钟");
                        }

                        if (mCurrentProgress < mTotalNumber) {
                            mCurrentProgress++;
                            pbSign.setProgress(mCurrentProgress);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        close();
                    }

                    @Override
                    public void onComplete() {
                        close();
                    }
                });
    }

    /**
     * 防止泄露
     */
    private void close() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }

    @OnClick({R.id.ib_bar_back, R.id.bt_finish, R.id.bt_skip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_bar_back:
                finish();
                break;
            case R.id.bt_finish:
            case R.id.bt_skip:
                sendToService();
                Intent intent = new Intent(this, CoursePlayActivity.class);
                intent.putExtra(Constant.COURSE,mCourse);
                intent.putExtra(Constant.TOTAL_NUMBER,mTotalNumber);
                startActivity(intent);
                finish();
                break;
            default:
        }
    }

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

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

                    }
                });
    }

    @Override
    protected void onPause() {
        super.onPause();
        close();
    }
}
