package jp.odakyu.toiletsignage.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;

import jp.odakyu.toiletsignage.application.ToiletApplication;
import jp.odakyu.toiletsignage.info.ToiletInfo;

/**
 * Created by agentec on 2017/12/11.
 */

public class ParentActivity extends AppCompatActivity {
    private final String TAG = "ParentActivity";

    Handler mHandler;
    ToiletInfo toiletInfo;

    BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //横画面
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        //ステータスバー非表示
        Window window = getWindow();
        View view = window.getDecorView();
        view.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_FULLSCREEN);

        //BackgroundThreadからMainThread使用するため
        mHandler = new Handler();

        toiletInfo = ToiletInfo.getInstance();

        //BroadcastReceiver設定
        IntentFilter filter = new IntentFilter(ToiletInfo.NAME_CONNTENT_FINISH);
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                reloadDisplayView();
            }
        };
        registerReceiver(broadcastReceiver, filter);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

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

    @Override
    protected void onStop() {
        ToiletApplication application = (ToiletApplication) getApplication();
        //アプリがバックグラウンドのなった時のみタスクを停止
        if (!application.isForegrdound(this)) {
            Log.i(TAG, "Application Background");
            application.stopTimer();
        }

        super.onStop();
    }

    @Override
    protected void onStart() {
        ToiletApplication application = (ToiletApplication) getApplication();
        //初回起動時以外はタスク開始（バックグラウンドからフォアグラウンド対応）
        if (toiletInfo.getToiletDefaultJsonArray() != null) {
            application.startTimer();
        }
        super.onStart();
    }

    //Child Classから実装
    public void reloadDisplayView() {}
}
