package com.zkjc.common.base;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zkjc.common.utils.BrightnessTools;
import com.zkjc.common.utils.StatusBarUtils;
import com.zkjc.utils.R;

public class ScreenActivity extends AppCompatActivity {
    private static final int MSGKEY = 0x10001;
    private TextView time;

    private long exitTime = 0;
    private RelativeLayout root;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏导航栏全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_screen);
        StatusBarUtils.with(this).init();
        root = findViewById(R.id.root);
        time = findViewById(R.id.time);
        time.setSelected(true);
        root.setBackgroundResource(R.drawable.img_list);
        animationDrawable = (AnimationDrawable) root.getBackground();
        animationDrawable.start();
        int screenBrightness = BrightnessTools.getScreenBrightness(this);
        Log.d("ScreenActivity", "onCreate: "+screenBrightness);
        if (screenBrightness>50){
            BrightnessTools.setBrightness(this,0);
        }
        // new TimeThread().start();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (animationDrawable != null && animationDrawable.isRunning())
            animationDrawable.stop();
    }
    /* public class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    //更新时间
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = MSGKEY;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }*/

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                finish();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 2秒内连续两次按下回车键退出屏保
     * 重写方法
     *
     * @param keyCode
     * @param event
     * @return
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次解锁键即可进入应用", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

   /* private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSGKEY:
                    long sysTime = System.currentTimeMillis();
                    CharSequence sysTimeStr = DateFormat.format("yyyy-MM-dd hh:mm", sysTime);
                    time.setText(sysTimeStr);
                    break;

            }
        }
    };*/


}
