package com.sunzee.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.base.MvpActivity;
import com.sunzee.model.Heartbeat;
import com.sunzee.model.message.ApiMessageEvent;
import com.sunzee.model.message.DownLoadMessageEvent;
import com.sunzee.mvp.advertising.AdvertisingPresenter;
import com.sunzee.mvp.advertising.AdvertisingView;
import com.sunzee.ui.dialog.WarringDialog;
import com.sunzee.utils.AlarmManagerUtil;
import com.sunzee.utils.GeTuiUtil;
import com.sunzee.utils.LongClickUtils;
import com.sunzee.utils.SimplePlayerUtils;
import com.wuxiaolong.androidutils.library.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 广告界面 activity
 */
public class AdvertisingActivity extends MvpActivity<AdvertisingPresenter> implements AdvertisingView {


    private static final String TAG = "AdvertisingActivity";
    private SurfaceView mAdSurfaceView;
    private SurfaceView mAdbSurfaceView;
    private SimplePlayerUtils mRightsimplePlayer;
    private SimplePlayerUtils mLeftsimplePlayer;
    private ImageView mAdImageview;
    private ImageView mAdbImageview;
    private GeTuiUtil mGeTuiUtil;
    private RelativeLayout mRlRoot;
    private TextView mTvTime;
    private MyHandler myHandler;
    private TextView mTvEquipmentNo;
    private WarringDialog warringDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertising);
        EventBus.getDefault().register(this);
        mGeTuiUtil = new GeTuiUtil(this);
        mGeTuiUtil.geTui();
        initView();
        initEvent();
        AlarmManagerUtil.setAlarm(this, 1, 12, 16, 0, 6, 0, "闹钟响了32");
    }

    private void initEvent() {
        LongClickUtils.setLongClick(new Handler(), mRlRoot, 10, new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(AdvertisingActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                return false;
            }
        });
    }

    private void initView() {
        myHandler = new MyHandler(this);
        mAdSurfaceView = findViewById(R.id.sf_cor_ad);
        mAdImageview = findViewById(R.id.iv_cor_ad);
        mAdbSurfaceView = findViewById(R.id.sf_cor_adb);
        mAdbImageview = findViewById(R.id.iv_cor_adb);
        mRlRoot = findViewById(R.id.rl_root);
        mTvTime = findViewById(R.id.tv_time1);
        mTvEquipmentNo = findViewById(R.id.tv_equipment_no1);
        Log.d(TAG, "initView: " + Heartbeat.deviceId);
        if (Heartbeat.deviceId != null && Heartbeat.deviceId.length() > 7) {
            mTvEquipmentNo.setText(Heartbeat.deviceId != null ? Heartbeat.deviceId.substring(Heartbeat.deviceId.length() - 6) : "");

        }
        //1.初始化播放器
        mRightsimplePlayer = new SimplePlayerUtils(mAdSurfaceView);
        mRightsimplePlayer.setImageView(mAdImageview);
        mLeftsimplePlayer = new SimplePlayerUtils(mAdbSurfaceView);
        mLeftsimplePlayer.CloseVolume();
        mLeftsimplePlayer.setImageView(mAdbImageview);
        LogUtil.d(TAG, "setup: " + mRightsimplePlayer);
        LogUtil.d(TAG, "setup: " + mLeftsimplePlayer);
        play();
        startRefreshime();
    }

    private void play() {
        mvpPresenter.play();
    }


    @Override
    public void playLeft(ArrayList<File> playFileList) {
        mLeftsimplePlayer.setFileData(playFileList);
        mLeftsimplePlayer.playVideo();
    }

    @Override
    public void playRight(ArrayList<File> playFile) {
        mRightsimplePlayer.setFileData(playFile);

        mRightsimplePlayer.playVideo();
    }

    @Override
    public void updateTime(CharSequence sysTimeStr) {
        mTvTime.setText(sysTimeStr);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mLeftsimplePlayer.onDestroy();
        mRightsimplePlayer.onDestroy();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        myHandler.removeCallbacksAndMessages(null);
        myHandler = null;
        stopRefreshTime();
    }

    //个推相关
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(DownLoadMessageEvent messageEvent) {
        switch (messageEvent.getType()) {
            case downloadAll:
                play();
                break;
            default:
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(ApiMessageEvent messageEvent) {
        switch (messageEvent.getName()) {
            case "pushTimeRule":
                //推送最新规则
                LogUtil.d(TAG, "pushTimeRule:");
                LogUtil.e(TAG, "event: 接收到规则");
                play();
                break;
        }
    }

    @Override
    protected AdvertisingPresenter createPresenter() {
        return new AdvertisingPresenter(this);
    }

    public static class MyHandler extends Handler {
        //持有弱引用HandlerActivity,GC回收时会被回收掉.
        private final WeakReference<AdvertisingActivity> mActivty;

        public MyHandler(AdvertisingActivity activity) {
            mActivty = new WeakReference<AdvertisingActivity>(activity);
        }

        @Override
        public void handleMessage(android.os.Message msg) {
            AdvertisingActivity activity = mActivty.get();
            switch (msg.what) {
                case 1:
                    long sysTime = System.currentTimeMillis();//获取系统时间
                    CharSequence sysTimeStr = DateFormat.format("yyyy-MM-dd HH:mm:ss", sysTime);//时间显示格式
                    activity.mTvTime.setText(sysTimeStr);
                    break;
            }
        }
    }

    private Timer timerGetTime;
    private TimerTask timerGetTimerTak;

    private void startRefreshime() {
        timerGetTime = new Timer();
        timerGetTimerTak = new TimerTask() {
            @Override
            public void run() {
                myHandler.sendEmptyMessage(1);
            }
        };
        timerGetTime.schedule(timerGetTimerTak, 0, 1000);
    }

    private void stopRefreshTime() {
        if (timerGetTime != null) {
            timerGetTime.cancel();
            timerGetTime = null;
        }
        if (timerGetTimerTak != null) {
            timerGetTimerTak.cancel();
            timerGetTimerTak = null;
        }
    }
}
