package com.colorful.keyboard.theme.myutil;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.Nullable;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.colorful.keyboard.theme.App;
import com.colorful.keyboard.theme.latin.LatinIME;

import java.io.File;

public class ConfigBitmapUtils {

    public static void saveConfigImgToLocal(Context context, String url) {
        Glide.with(context)
                .downloadOnly()
                .load(url)
                .listener(new RequestListener<File>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
                        return false;
                    }


                    @Override
                    public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
                        configSaveToAlbum(context, resource.getAbsolutePath());
                        return false;
                    }
                })
                .preload();


    }

    public static void configSaveToAlbum(Context context, String srcPath) {
        String imageSrc = context.getExternalFilesDir("input").getAbsolutePath() +"image/"+ System.currentTimeMillis() + ".png";
        File file = new File(imageSrc);
        SPUtils.getInstance().put("bg",imageSrc);

        App.Companion.getApp().stopService(new Intent(App.Companion.getApp(),LatinIME.class));
        App.Companion.getApp().startService(new Intent(App.Companion.getApp(),LatinIME.class));


        boolean isCopySuccess = false;
        try {
            isCopySuccess = ConfigFileUtils.configCopy(srcPath, file.getAbsolutePath(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*if (isCopySuccess) {
            try {
                MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        file.getAbsolutePath(),file.getName() , null);
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));
                Toast.makeText(context,"successfully",Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } else {

        }*/
    }


}
