package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.NewbieTaskRecord;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.RxjavaCountDownTimer;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

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

/**
 * 一分钟快速了解小绵羊
 * Created by realicing on 2018/4/18.
 */

public class ActUnderstandSheep extends BaseActivity {

    private Activity activity;

    @BindView(R.id.i_understand_tv)
    TextView i_understand_tv;
    @BindView(R.id.understand_sheep_01)
    ImageView understand_sheep_01;
    @BindView(R.id.understand_sheep_02)
    ImageView understand_sheep_02;
    @BindView(R.id.understand_sheep_03)
    ImageView understand_sheep_03;
    @BindView(R.id.understand_sheep_04)
    ImageView understand_sheep_04;
    @BindView(R.id.understand_sheep_05)
    ImageView understand_sheep_05;

    private RxjavaCountDownTimer timer;
    @Override
    protected int getLayoutId() {
        return R.layout.act_understand_sheep;
    }

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils.getInstance()
                .setTitle(this, "一分钟快速了解小绵羊");
        activity.findViewById(R.id.img_baseactivity_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.finish();
                setDontShowNewbieTaskDialog();
            }
        });
        GlideImageLoader.setImage(understand_sheep_01, "http://cdngame.kuaifazs.com/understand_sheep_01.png");
        GlideImageLoader.setImage(understand_sheep_02, "http://cdngame.kuaifazs.com/understand_sheep_02.png");
        GlideImageLoader.setImage(understand_sheep_03, "http://cdngame.kuaifazs.com/understand_sheep_03.png");
        GlideImageLoader.setImage(understand_sheep_04, "http://cdngame.kuaifazs.com/understand_sheep_04.png");
        GlideImageLoader.setImage(understand_sheep_05, "http://cdngame.kuaifazs.com/understand_sheep_05.png");
        newbie_task_record = (NewbieTaskRecord) getIntent().getSerializableExtra(NewbieTaskRecord.class.getSimpleName());
        final int count = BuildConfig.DEBUG ? 10 : 15;
        timer = RxjavaCountDownTimer.getInstance(count)
                .setOnTickListener(new RxjavaCountDownTimer.OnTickListener() {
                    @Override
                    public void onFinish() {
                        i_understand_tv.setEnabled(true);
                        i_understand_tv.setText("我知道了");
                    }

                    @Override
                    public void onTicker(long time) {
                        i_understand_tv.setEnabled(false);
                        i_understand_tv.setText(activity.getString(R.string.tip_newbie_task_1, time));
                    }
                }).start();
    }
    private NewbieTaskRecord newbie_task_record;
    private void setDontShowNewbieTaskDialog() {
        if(newbie_task_record != null){
            newbie_task_record.setDontShowNewbieTaskDialog(true);
            MyDbManager.getInstance().saveOrUpdate(newbie_task_record);
        }
    }

    @Override
    public void initListener() {
        i_understand_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SheepApp.getInstance().getNetComponent().getApiService().appNewbieTaskLearnFinish()
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                                    @Override
                                    public void onNext(BaseMessage baseMessage) {
                                        finish();
                                    }

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

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setDontShowNewbieTaskDialog();
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer != null)
            timer.clear();
    }
}
