package com.oo.eye.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.LogUtil;
import com.app.baselibrary.commonUtil.NibiruUtils;
import com.oo.eye.R;
import com.oo.eye.R2;
import com.oo.eye.manager.DaemonSender;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 */

public class BeforeEyeTestActivity extends BaseActivity {

    @BindView(R.id.ll_before_eye_start1)
    RelativeLayout mLlBeforeEyeStart1;
    @BindView(R.id.ll_before_eye_prepare1)
    LinearLayout mLlBeforeEyePrepare1;
//    private DaemonReceiver mDaemonReceiver;

    private ActionReceiver mActionReceiver;

    private Handler mHandler;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_before_eye_test;
    }

    @Override
    protected void init() {
        mHandler = new Handler();
//        mDaemonReceiver = new DaemonReceiver();
//        mDaemonReceiver.registerScreenActionReceiver(this);
        mActionReceiver = new ActionReceiver();
        mActionReceiver.registerScreenActionReceiver(this);
        DaemonSender.startEyeTest(this);
    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DaemonSender.closeEyeTestMotor(this);
//        if (mDaemonReceiver != null) {
//            mDaemonReceiver.unRegisterScreenActionReceiver(this);
//        }
        if (mActionReceiver != null) {
            mActionReceiver.unRegisterScreenActionReceiver(this);
        }
    }

    @OnClick(R2.id.tv_eye_test_start1)
    public void click(View view) {
        mLlBeforeEyeStart1.setVisibility(View.GONE);
        mLlBeforeEyePrepare1.setVisibility(View.VISIBLE);
        speak("调节测视距离，请等待");
//        DaemonSender.startEyeTestMotor(this);
		Intent intent2 = new Intent(BeforeEyeTestActivity.this,EyeTestActivity.class);
		startActivity(intent2);
		finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

//    public class DaemonReceiver extends BroadcastReceiver {
//        private boolean isRegisterReceiver = false;
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent != null) {
//                int type = intent.getIntExtra(OOMsg.ARG_SEEX_ACTION, 0);
//                switch (type) {
//                    case OOMsg.EYE_TEST_MOTOR_FINISH:
//                        mHandler.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                Intent intent2 = new Intent(BeforeEyeTestActivity.this, EyeTestActivity.class);
//                                startActivity(intent2);
//                                finish();
//                            }
//                        }, 1000);
//                        break;
//                }
//
//            }
//
//        }
//
//        public void registerScreenActionReceiver(Context mContext) {
//            if (!isRegisterReceiver) {
//                isRegisterReceiver = true;
//
//                IntentFilter filter = new IntentFilter();
//                filter.addAction(OOMsg.OO_INTENT_ACTION);
//                mContext.registerReceiver(DaemonReceiver.this, filter);
//            }
//        }
//
//        public void unRegisterScreenActionReceiver(Context mContext) {
//            if (isRegisterReceiver) {
//                isRegisterReceiver = false;
//                mContext.unregisterReceiver(DaemonReceiver.this);
//            }
//        }
//    }

    class ActionReceiver extends BroadcastReceiver {

        private String TAG = "TrainTaskService__ActionReceiver";
        private boolean isRegisterReceiver = false;

        @Override
        public void onReceive(final Context context, Intent intent) {
            String action = intent.getAction();
            LogUtil.e(TAG, "action..." + action);
            if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                //如果，灭屏，就进行暂停操作。
                mLlBeforeEyeStart1.setVisibility(View.VISIBLE);
                mLlBeforeEyePrepare1.setVisibility(View.GONE);
            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
                mLlBeforeEyeStart1.setVisibility(View.VISIBLE);
                mLlBeforeEyePrepare1.setVisibility(View.GONE);
            }

        }

        public void registerScreenActionReceiver(Context mContext) {
            if (!isRegisterReceiver) {
                isRegisterReceiver = true;

                IntentFilter filter = new IntentFilter();
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                filter.addAction(Intent.ACTION_SCREEN_ON);
                filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                LogUtil.e(TAG, "reg broadcast...");
                mContext.registerReceiver(ActionReceiver.this, filter);
            }
        }

        public void unRegisterScreenActionReceiver(Context mContext) {
            if (isRegisterReceiver) {
                isRegisterReceiver = false;
                LogUtil.e(TAG, "unReg broadcast...");
                mContext.unregisterReceiver(ActionReceiver.this);
            }
        }
    }

    private void speak(String text) {
        DaemonSender.sendDBTTS(this, text);
    }


}
