package ai.pai.lensman.splash;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;

import com.android.common.utils.NetworkUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.service.UpgradeService;

/**
 * Created by chengzhenyu on 2016/8/14.
 */
public class SplashPresenter implements SplashContract.Presenter {

    private Context context;
    private SplashContract.View view;
    private Handler mHandler;
    private static final int NORMAL_DELAY_TIME = 1500;

    public SplashPresenter(Context context, SplashContract.View view){
        this.view = view;
        this.context = context;
        mHandler = new Handler();
    }

    @Override
    public void start() {
        if(TextUtils.isEmpty(Preferences.getInstance().getLensManId())){
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.jump2Login();
                }
            }, NORMAL_DELAY_TIME);
            return;
        }
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                view.jump2Main();
            }
        }, NORMAL_DELAY_TIME);
        String curDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if(Preferences.getInstance().getLastRunDate().equals(curDate)){
            return;
        }
//        checkUpdate();
        Preferences.getInstance().setLastRunDate(curDate);
    }

    @Override
    public void stop() {
        mHandler.removeCallbacksAndMessages(null);
    }

    private void checkUpdate() {
        if(NetworkUtil.isWifiConnected(context)){
            Intent intent = new Intent(context, UpgradeService.class);
            context.startService(intent);
        }
    }

}
