package com.zhaohaoting.framework.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * author：Created by zhaohaoting on 2019/6/11
 * email：526309416@qq.com
 * desc：
 */
public class ActivityUtils {
    private Intent intent = new Intent();
    private Activity activity;
    private Fragment fragment;

    private ActivityUtils(){}
    public static ActivityUtils newInstance(Activity activity) {
        return new ActivityUtils(activity);
    }

    public static ActivityUtils newInstance(Fragment fragment) {
        return new ActivityUtils(fragment);
    }

    private ActivityUtils(Activity activity) {
        this.activity = activity;
    }

    private ActivityUtils(Fragment fragment) {
        this.fragment = fragment;
    }

    public void navigation(Class<? extends Activity> toActivity) {
        if (fragment != null) {
            Context context = fragment.getContext();
            if (context != null) {
                intent.setClass(context, toActivity);
                fragment.startActivity(intent);
            }
        } else if (activity != null) {
            intent.setClass(activity, toActivity);
            activity.startActivity(intent);
        }
    }

    public void navigation(Class<? extends Activity> toActivity, int requestCode) {
        if (fragment != null) {
            Context context = fragment.getContext();
            if (context != null) {
                intent.setClass(context, toActivity);
                fragment.startActivityForResult(intent, requestCode);
            }
        } else if (activity != null) {
            intent.setClass(activity, toActivity);
            activity.startActivityForResult(intent, requestCode);
        }
    }

    public ActivityUtils putExtra(String name, boolean value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, byte value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, char value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, short value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, int value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, long value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, float value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, double value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, String value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, CharSequence value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, Parcelable value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) {
        intent.putParcelableArrayListExtra(name, value);
        return this;
    }

    public ActivityUtils putIntegerArrayListExtra(String name, ArrayList<Integer> value) {
        intent.putIntegerArrayListExtra(name, value);
        return this;
    }

    public ActivityUtils putStringArrayListExtra(String name, ArrayList<String> value) {
        intent.putStringArrayListExtra(name, value);
        return this;
    }

    public ActivityUtils putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value) {
        intent.putCharSequenceArrayListExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, Serializable value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, boolean[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, byte[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, short[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, char[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, int[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, long[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, float[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, double[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, String[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtra(String name, CharSequence[] value) {
        intent.putExtra(name, value);
        return this;
    }

    public ActivityUtils putExtras(Bundle bundle) {
        intent.putExtras(bundle);
        return this;
    }

    public ActivityUtils replaceExtras(Intent src) {
        intent.replaceExtras(src);
        return this;
    }

    public ActivityUtils replaceExtras(Bundle bundle) {
        intent.replaceExtras(bundle);
        return this;
    }

    public ActivityUtils removeExtra(String name) {
        intent.removeExtra(name);
        return this;
    }

    public ActivityUtils setFlags(int flags) {
        intent.setFlags(flags);
        return this;
    }

    public ActivityUtils addFlags(int flags) {
        intent.addFlags(flags);
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public ActivityUtils removeFlags(int flags) {
        intent.removeFlags(flags);
        return this;
    }

    public ActivityUtils setPackage(String packageName) {
        intent.setPackage(packageName);
        return this;
    }

    public ActivityUtils setComponent(ComponentName component) {
        intent.setComponent(component);
        return this;
    }

    public ActivityUtils setClassName(Context packageContext, String className) {
        intent.setClassName(packageContext, className);
        return this;
    }

    public ActivityUtils setClassName(String packageName, String className) {
        intent.setClassName(packageName, className);
        return this;
    }

    public ActivityUtils setClass(Context packageContext, Class<?> cls) {
        intent.setClass(packageContext, cls);
        return this;
    }

    public ActivityUtils setUrl(Uri Uri) {
        intent.setData(Uri);
        return this;
    }
}
