package com.kfzs.cfyl.media;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.kfzs.cfyl.media.dialog.DialogProgress;
import com.kfzs.cfyl.media.util.G;

import java.lang.reflect.Field;

/**
 * Created by kemllor on 2017/12/15.
 */

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        G.init(getResources());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setCustomView(R.layout.media_title);
            actionBar.hide();
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setElevation(0);
            Toolbar toolbar = (Toolbar) actionBar.getCustomView().getParent();
            toolbar.setContentInsetsRelative(0, 0);
        }
        setContentView(getLayoutId());
        initView();
        initListener();
        initData();
    }

    protected abstract int getLayoutId();

    public abstract void initView();

    public void initListener() {

    }

    public void initData() {

    }

    protected DialogProgress dialog;
    public boolean isShowing() {
        return dialog != null && dialog.getAlertDialog() != null && dialog.getAlertDialog().isShowing();
    }

    public DialogProgress showProgress() {
        hideProgress();
        dialog = DialogProgress.showDialog(this);
        return dialog;
    }

    protected DialogProgress showProgress(boolean cancel) {
        hideProgress();
        dialog = DialogProgress.showDialog(this, cancel);
        return dialog;
    }


    public void hideProgress() {
        if (isShowing()) {
            try {
                dialog.getAlertDialog().dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
