package com.sipai.jspatrol.ShowNotification;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.sipai.jspatrol.Main.MainActivity;
import com.sipai.jspatrol.R;

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

public class LockScreenDialogActivity extends Activity {

    private MediaPlayer mp;

    public static void actionStart(Context context,String title,String content){
        Intent intent = new Intent(context,LockScreenDialogActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("content",content);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        setContentView(R.layout.activity_lock_screen_dialog);
        setRing();
        TextView tv_app = findViewById(R.id.moudle_alarmhandler_tv_app);
        tv_app.setText(getString(R.string.app_name));
        TextView tVinfo=(TextView)findViewById(R.id.tv_info);
        tVinfo.setText(this.getIntent().getStringExtra("content"));
        TextView tvtitle = findViewById(R.id.moudle_alarmhandler_tv_title);
        tvtitle.setText(this.getIntent().getStringExtra("title"));
        tVinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
                KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
                keyguardLock.disableKeyguard();
                Intent alarmIntent = new Intent(LockScreenDialogActivity.this, MainActivity.class);
                alarmIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                startActivity(alarmIntent);
                finish();
            }
        });
        ImageView imdelete=(ImageView)findViewById(R.id.iv_delete);
        imdelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        Date curDate  = new  Date(System.currentTimeMillis());//获取当前时间
        String str  =  formatter.format(curDate);
        TextView tvtime=(TextView)findViewById(R.id.tv_time);
        tvtime.setText(str);
    }

    private void setRing() {
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
//		backgroundMusic.playBackgroundMusic("alert.wav", true);
        try {
            mp = new MediaPlayer();
            mp.setDataSource(this,uri);
            mp.prepare();
            mp.setLooping(true);
            if (!mp.isPlaying()) {
                mp.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mp!=null&&mp.isPlaying()) {
            mp.stop();
            mp.release();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        if (!pm.isScreenOn()) {
            @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
            wl.acquire();
            wl.release();
        }
    }
}