package com.gh.gamecenter.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.gh.common.util.RunningUtils;
import com.gh.gamecenter.SplashScreenActivity;

/**
 * Created by khy on 2016/9/1.
 */
public class ActivitySkipReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.gh.gamecenter.ACTIVITYSKIP")) {
            Bundle bundle = intent.getExtras();
            if (RunningUtils.isRunning(context)) {
                // 应用正在运行，前台或后台
                String to = bundle.getString("to");
                if (!TextUtils.isEmpty(to)) {
                    Intent intent1 = new Intent(context, intentClass(to));
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intentControl(intent1, bundle);
                    context.startActivity(intent1);
                }
            } else {
                // 应用未在运行
                Intent intent1 = new Intent(context, SplashScreenActivity.class);
                intent1.setAction(Intent.ACTION_MAIN);
                intent1.addCategory(Intent.CATEGORY_LAUNCHER);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intentControl(intent1, bundle);
                context.startActivity(intent1);
            }
        }
    }

    private Class<?> intentClass(String to) {
        try {
            return Class.forName("com.gh.gamecenter." + to);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void intentControl(Intent intent, Bundle bundle) {
        String to = bundle.getString("to");
        if ("NewsActivity".equals(to)) {
            intent.putExtra("newsId", bundle.getString("newsId"));
            intent.putExtra("entrance", bundle.getString("entrance"));
        } else if("DownloadManagerActivity".equals(to)) {
            intent.putExtra("packageName", bundle.getString("packageName"));
        } else if ("GameDetailsActivity".equals(to)) {
            intent.putExtra("gameId", bundle.getString("gameId"));
            intent.putExtra("entrance", bundle.getString("entrance"));
        } else if ("SubjectActivity".equals(to)) {
            intent.putExtra("id", bundle.getString("id"));
            intent.putExtra("name", bundle.getString("name"));
            intent.putExtra("order", bundle.getBoolean("order"));
        } else if ("ViewImageActivity".equals(to)) {
            intent.putExtra("urls", bundle.getStringArrayList("urls"));
            intent.putExtra("current", bundle.getInt("current",0));
            intent.putExtra("ScaleType", bundle.getString("ScaleType"));
        }
        if ("NewsActivity".equals(to)) {
            intent.putExtra("to", "NewsDetailActivity");
        } else if ("GameDetailsActivity".equals(to)) {
            intent.putExtra("to", "GameDetailActivity");
        } else {
            intent.putExtra("to", to);
        }
    }

}
