package com.hh.arome.view;

import static com.hh.lib_base.Constants.Value.TIME_HEARTBEAT;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.hh.arome.R;
import com.hh.lib_base.Constants;
import com.hh.lib_base.utils.device.DeviceUtils;
import com.hh.lib_common.utils.DateUtil;

import java.util.Date;

/**
 * Description: $
 *
 * @author xwh
 * time 2024/8/21
 **/
public class CurrentTimeView extends LinearLayout implements LifecycleObserver {

    private Handler mHandler;
    private Runnable runnableTime;
    private Runnable runnableHeartbeat;
    private OnTimeListener listener;


    public CurrentTimeView(Context context) {
        super(context);
        init(context);
    }

    public CurrentTimeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CurrentTimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public CurrentTimeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }


    private TextView tvTime;

    private void init(Context context) {
        setOrientation(VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.view_home_current_time, this);
        tvTime = findViewById(R.id.tv_time);
        String deviceModel = DeviceUtils.getDeviceModel();
        if (Constants.DeviceModel.D2.equals(deviceModel)) {
            tvTime.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        }
        getmHandler().post(getRunnableTime());
        getmHandler().postDelayed(getRunnableHeartbeat(), TIME_HEARTBEAT);
    }


    /**
     *
     * @author xwh
     * Time 2024/8/21
     * Description: 当前时间
     *
    **/
    public Runnable getRunnableTime() {
        if (runnableTime == null) {
            synchronized (CurrentTimeView.class) {
                if (runnableTime == null) {
                    runnableTime = () -> {
                        // 设置最新时间
                        tvTime.setText(DateUtil.getTime("yyyy.MM.dd HH:mm:ss")
                                + " " + DateUtil.getWeekOfDate(new Date()));
                        // 接着发送刷新任务
                        getmHandler().postDelayed(runnableTime, 1000);
                    };
                }
            }
        }
        return runnableTime;
    }

    /**
     *
     * @author xwh
     * Time 2024/8/21
     * Description: APP心跳
     *
    **/
    public Runnable getRunnableHeartbeat() {
        if (runnableHeartbeat == null) {
            synchronized (CurrentTimeView.class) {
                if (runnableHeartbeat == null) {
                    runnableHeartbeat = () -> {
                        getmHandler().postDelayed(runnableHeartbeat, TIME_HEARTBEAT);
                        if(listener!=null){
                            listener.onHeartbeat();
                        }
                    };
                }
            }
        }
        return runnableHeartbeat;
    }

    public Handler getmHandler() {
        if (mHandler == null) {
            synchronized (CurrentTimeView.class) {
                if (mHandler == null) {
                    mHandler = new Handler(Looper.getMainLooper());
                }
            }
        }
        return mHandler;
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }


    public void setOnTimeListener(OnTimeListener listener){
        this.listener=listener;
    }
    public interface OnTimeListener{
       void onHeartbeat();
     //  void onShowMiniProgram();

    }

}
