package com.smartcar.pvrmemoryleakdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class MainActivity extends Activity implements View.OnClickListener {

    private View mSwitchWindow = null;
    private FrameLayoutEx mPopupView = null;
    private SCPopupWindow mSCPopupWindow = null;
    private SCPopupWindow mTabPopupWindow = null;
    private FrameLayoutEx mTabPopupView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSwitchWindow = findViewById(R.id.switch_window);
        mSwitchWindow.setOnClickListener(this);
        SystemClock.uptimeMillis();
        initPopupWindow();
        initTabWindow();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.switch_window:
                if (mTabPopupWindow.isShowing()) {
                    dismissTab();
                    showPopupWindow();
                } else {
                    dismiss();
                    showTabPopupWindow();
                }
            default:
                break;
        }
    }

    private void initPopupWindow() {
        if (mSCPopupWindow == null) {
            mPopupView = (FrameLayoutEx) LayoutInflater.from(this).inflate(R.layout.sc_statusbar_right_popup_window, null);
            mPopupView.setTouchEvent(new FrameLayoutEx.OnTouchEventLinster() {
                @Override
                public void onClose() {
                    dismiss();
                }
            });
            mSCPopupWindow = new SCPopupWindow(this, mPopupView, 800, 800);
            mSCPopupWindow.setThouchOutSide(false);
        }
    }

    public void dismiss() {
        if (mSCPopupWindow != null) {
            mSCPopupWindow.dismiss();
        }
    }

    private void showPopupWindow() {
        if (mSCPopupWindow == null) {
            initPopupWindow();
        }
        mSCPopupWindow.showWindow(mSwitchWindow, 44, -10);
    }

    private void initTabWindow() {
        System.currentTimeMillis();
        if (mTabPopupWindow == null) {
            mTabPopupView = (FrameLayoutEx) LayoutInflater.from(this).inflate(R.layout.layout_tab_main, null);
            mTabPopupView.setTouchEvent(new FrameLayoutEx.OnTouchEventLinster() {
                @Override
                public void onClose() {
                    dismissTab();
                }
            });
            mTabPopupWindow = new SCPopupWindow(this, mTabPopupView, 800, 800);
            mTabPopupWindow.setThouchOutSide(false);
            new TabController(this,mTabPopupView);
        }
    }

    private void showTabPopupWindow() {
        if (mTabPopupWindow == null) {
            initTabWindow();
        }
        mTabPopupWindow.showWindow(mSwitchWindow, 44, -10);
    }

    public void dismissTab() {
        if (mTabPopupWindow != null) {
            mTabPopupWindow.dismiss();
        }
    }

}
