package com.cariad.cea.aircondition.views;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.cariad.cea.aircondition.R;

public class SimulationWindLayout extends ConstraintLayout {
    private View rootView;
    private SimulationWindBottomView imgSimWindBottom;
    private SimulationWindTopView imgSimWindTop;
    private AnimationDrawable bottomAnimation;
    private AnimationDrawable topAnimation;

    public SimulationWindLayout(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public SimulationWindLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public SimulationWindLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public SimulationWindLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context, attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs) {
        View.inflate(context, R.layout.layout_simulation_wind, this);
        rootView = findViewById(R.id.layout_sim_wind);
        imgSimWindBottom = findViewById(R.id.img_sim_wind_bottom);
        imgSimWindTop = findViewById(R.id.img_sim_wind_top);
        // 启动线程执行动画操作,防止因为动画资源图片太多导致界面卡顿的问题
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (bottomAnimation == null) {
                        bottomAnimation = (AnimationDrawable) imgSimWindBottom.getDrawable();
                    }
                    if (!bottomAnimation.isRunning()) {
                        bottomAnimation.start();
                    }
                    if (topAnimation == null) {
                        topAnimation = (AnimationDrawable) imgSimWindTop.getDrawable();
                    }
                    if (!topAnimation.isRunning()) {
                        topAnimation.start();
                    }
                    handler.sendEmptyMessage(0x101);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        // 设置rootView的touch事件
        rootView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                imgSimWindBottom.onTouchEvent(event);
                imgSimWindTop.onTouchEvent(event);
                return false;
            }
        });
    }

    public void startAnimation() {
        if (bottomAnimation == null) {
            bottomAnimation = (AnimationDrawable) imgSimWindBottom.getDrawable();
        }
        if (!bottomAnimation.isRunning()) {
            bottomAnimation.start();
        }
        if (topAnimation == null) {
            topAnimation = (AnimationDrawable) imgSimWindTop.getDrawable();
        }
        if (!topAnimation.isRunning()) {
            topAnimation.start();
        }
    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x101) {
                startAnimation();
            }
        }
    };
}
