package com.danding.yang800;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.danding.yang800.General.Yang800App;
import com.danding.yang800.Presenter.BaseActivityPresenter;
import com.danding.yang800.Reciver.NetBroadCastReciver;
import com.danding.yang800.Service.LoginKeepService;
import com.danding.yang800.Util.NetUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Iterator;

public class BaseActivity extends AppCompatActivity implements NetBroadCastReciver.NetEvevt {
    public static NetBroadCastReciver.NetEvevt evevt;
    protected static ArrayList<BaseActivity> activityLists = new ArrayList<BaseActivity>();
    private int netMobile;
    SharedPreferences sp;
    BaseActivityPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        activityLists.add(this);
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        presenter = new BaseActivityPresenter(this);
        evevt = this;
//        inspectNet();
//        boolean netConnect = this.isNetConnect();
//        if (netConnect) {
//        } else {
//            ToastUtil.show(this, "当前无网络");
//        }
    }

    public boolean inspectNet() {
        this.netMobile = NetUtil.getNetWorkState(BaseActivity.this);

        return isNetConnect();

        // if (netMobile == NetUtil.NETWORK_WIFI) {
        // System.out.println("inspectNet：连接wifi");
        // } else if (netMobile == NetUtil.NETWORK_MOBILE) {
        // System.out.println("inspectNet:连接移动数据");
        // } else if (netMobile == NetUtil.NETWORK_NONE) {
        // System.out.println("inspectNet:当前没有网络");
        //
        // }
    }

    /**
     * 网络变化之后的类型
     */
    @Override
    public void onNetChange(int netMobile) {
        // TODO Auto-generated method stub

        this.netMobile = netMobile;
        //isNetConnect();
//        boolean netConnect = this.isNetConnect();
//        if (netConnect) {
//        } else {
//            ToastUtil.show(this, "当前无网络");【
//        }
    }

    /**
     * 判断有无网络 。
     *
     * @return true 有网, false 没有网络.
     */
    public boolean isNetConnect() {
        if (netMobile == NetUtil.NETWORK_WIFI) {
            return true;
        } else if (netMobile == NetUtil.NETWORK_MOBILE) {
            return true;
        } else if (netMobile == NetUtil.NETWORK_NONE) {
            return false;

        }
        return false;
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        MobclickAgent.onPause(this);
        super.onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        activityLists.remove(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        if (Yang800App.iflogin) {
            presenter.keepLogin();
        }
        if (sp.getBoolean("login", false)) {
            if (!isWorked("com.danding.yang800.Service.LoginKeepService")) {
                Intent startIntent = new Intent(
                        this,
                        LoginKeepService.class);
                startService(startIntent);
            }
        }
    }

    private boolean isWorked(String className) {
        ActivityManager myManager = (ActivityManager) this
                .getApplicationContext().getSystemService(
                        this.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(300);
        for (int i = 0; i < runningService.size(); i++) {
            if (runningService.get(i).service.getClassName().toString()
                    .equals(className)) {
                return true;
            }
        }
        return false;
    }

    protected void startActivity(Class<?> targetClass, int enterAnimID,
                                 int exitAnimId) {
        Intent intent = new Intent(this, targetClass);
        startActivity(intent);
        overridePendingTransition(enterAnimID, exitAnimId);

    }


    protected void startActivity(Class<?> targetClass) {
        Intent intent = new Intent(this, targetClass);
        startActivity(intent);
    }

    protected void startActivity(Class<?> targetClass, Bundle bundle) {
        Intent intent = new Intent(this, targetClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    protected void finish(int enterAnimID, int exitAnimId) {
        finish();
        overridePendingTransition(enterAnimID, exitAnimId);
    }

    public static void finishAll() {
        Iterator<BaseActivity> it = activityLists.iterator();
        while (it.hasNext()) {
            it.next().finish();
        }
    }

}
