package com.geek.demo.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.geek.demo.R;
import com.geek.demo.activity.WebActivity;

import java.util.List;

public class MyUtils {

    public static void loadImage(String logoUrl, ImageView imageView, Context context) {
        try {
            RequestOptions options = new RequestOptions();
            options.transform(new RoundedCorners(8));
            options.placeholder(R.drawable.service_default_logo);
            Glide.with(context).load(logoUrl).apply(options).into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void openUrl(Context context, String url) {
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("http")) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("url", url);
                context.startActivity(intent);
            } else {
                launchOtherApp(url, context);
            }
        }
    }

    public static void launchOtherApp(String url, Context context) {
        try {
            Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
            intent.addCategory("android.intent.category.BROWSABLE");
            PackageManager packageManager = context.getPackageManager();
            List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            if (resolveInfoList != null && resolveInfoList.size() > 0) {
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean incorrectTitle(String title) {
        return !TextUtils.isEmpty(title) && (title.contains(".jpg") || title.contains(".png") || title.contains(".gif") || title.contains("http://") || title.contains("https://"));
    }

}
