package com.mjsheng.myappstore.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.mjsheng.myappstore.comm.CommonDatas;
import com.mjsheng.myappstore.dialog.UpdateDialog;
import com.mjsheng.myappstore.utils.MySQLData;
import com.mjsheng.myappstore.utils.Utils;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;


public abstract class BaseActivity extends Activity implements OnClickListener {


    public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
    private UpdateReceiver mReceiver;
    private UpdateDialog updateDialog = null;
    private Bundle mSavedInstanceState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//关键代码
        mSavedInstanceState = savedInstanceState;
        initParameter();
        initViews();
        loadData();
    }

    protected Bundle getSavedInstanceState() {
        return mSavedInstanceState;
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    public class UpdateReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Utils.MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                String title = intent.getStringExtra(Utils.KEY_TITLE);
                if (title != null && !title.equals("")) {
                    if (title.equals("应用更新")) {
                        if (updateDialog == null) {
                            updateDialog = new UpdateDialog(BaseActivity.this, true);
                            updateDialog.show();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            //            initViewFull(this);
        }
    }

    public static void initViewFull(final Activity activity) {
        activity.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                initViewFull(activity);

                            }
                        });
                    }
                }.start();
            }
        });
        // //设置无标题
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activity.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }

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

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
        }
    }


    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_UI_HIDDEN) {
            Glide.get(this).clearMemory();
        }
        Glide.get(this).trimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Glide.get(this).clearMemory();
    }

    /**
     * 监听
     *
     * @param ids
     */
    public void setViewsListener(int... ids) {
        View v = null;
        for (int i = 0; i < ids.length; i++) {
            v = findViewById(ids[i]);
            v.setOnClickListener(this);
        }
    }

    protected void initParameter() {

        mReceiver = new UpdateReceiver();
        IntentFilter intent = new IntentFilter();
        intent.addAction(Utils.MESSAGE_RECEIVED_ACTION);
        intent.addAction(CommonDatas.BABYINFO_CHANGE);
        registerReceiver(mReceiver, intent);
    }

    protected void initViews() {
    }

    protected void loadData() {
    }

    // 启动系统桌面
    ;

    protected void startSystemLauncher() {
        try {
            if (startLauncher1("com.android.launcher.Launcher")) {
                return;
            }
            if ((!startLauncher1("com.android.launcher1.Launcher")) && (!startLauncher1("com.android.launcher2.Launcher"))
                    && (!startLauncher1("com.android.launcher3.Launcher"))
                    && (!startLauncher2("com.android.launcher3", "com.android.launcher3.Launcher"))
                    && (!startLauncher2("com.android.launcher4", "com.android.launcher4.Launcher"))) {
                Intent localIntent = new Intent("android.intent.action.MAIN");
                localIntent.addCategory("android.intent.category.HOME");
                startActivity(localIntent);
                return;
            }
        } catch (Exception localException) {

        }
    }

    private boolean startLauncher1(String pkg) {
        try {
            Intent intent = new Intent();
            intent.setClassName("com.android.launcher", pkg);
            startActivity(intent);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private boolean startLauncher2(String pkg1, String pkg2) {
        try {
            Intent intent = new Intent();
            intent.setClassName(pkg1, pkg2);
            startActivity(intent);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void finish() {
        super.finish();
    }

}
