/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package macampcorp.macamp.activities;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import macampcorp.macamp.ui.Sound;
import macampcorp.macamp.utils.Const;

public class CrashActivity extends AppCompatActivity {
    protected ImageView mImage;
    protected ImageView mBsod;
    protected View      mRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crash);
        mImage=findViewById(R.id.im_anim);
        mBsod=findViewById(R.id.im_bsod);
        mRoot=findViewById(R.id.ll_root);

        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(mImage, "scaleX", 0);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(mImage, "scaleY", 0);
        scaleDownX.setDuration(Const.POWER_OF_ANIM_DURATION);
        scaleDownY.setDuration(Const.POWER_OF_ANIM_DURATION);

        AnimatorSet scaleDown = new AnimatorSet();
        scaleDown.play(scaleDownX).with(scaleDownY);
        Sound.boom(this);
        scaleDown.start();

        final Activity t = this;
        final Handler handler = new Handler();
        final Runnable r = new Runnable()
        {
            public void run()
            {
                mRoot.setBackgroundColor(0xff0000fe);
                mBsod.setVisibility(View.VISIBLE);
                closeAfter(Const.BSOD_DURATION);
            }
        };
        handler.postDelayed(r, Const.POWER_OF_DURATION);
    }

    public void closeAfter(int x)
    {
        final Activity t = this;
        final Handler handler = new Handler();
        final Runnable r = new Runnable()
        {
            public void run()
            {
                finish();
            }
        };
        handler.postDelayed(r, x);
    }

    public static void start(Activity a)
    {
        Intent it = new Intent(a, CrashActivity.class);
        a.startActivity(it);
    }

    protected void hideActionBar()
    {
        View decorView = getWindow().getDecorView();
// Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
        ActionBar actionBar = getActionBar();
        actionBar.hide();
    }
}
