package com.kfzs.cfyl.media.util;

import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kfzs.cfyl.media.R;
import com.kfzs.cfyl.media.api.IFilter;
import com.kfzs.cfyl.media.glide.FilterTransform;

import java.util.Locale;

/**
 * Created by realicing on 2018/12/6.
 * realicing@sina.com
 */
public class ViewUtil {
    public static void setEnabled(View view, boolean enabled) {
        if (view != null) {
            view.setEnabled(enabled);
        }
    }

    public static void setVisibility(View view, boolean isVisible) {
        if (view != null) {
            view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        }
    }

    public static void toggleVisibility(View view) {
        if (view != null) {
            view.setVisibility(isVisible(view) ? View.GONE : View.VISIBLE);
        }
    }

    public static boolean isVisible(View view) {
        return view != null && view.getVisibility() == View.VISIBLE;
    }

    public static void setVisibility2(View view, boolean isVisible) {
        if (view != null) {
            view.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
        }
    }
    public static void setText(TextView textView) {
        if (textView != null) {
            textView.setText("");
        }
    }

    public static void setText(TextView textView, CharSequence msg) {
        if (textView != null) {
            textView.setText(TextUtils.isEmpty(msg) ? "" : msg);
        }
    }

    public static void setText(TextView textView, @StringRes int stringId, Object... objects) {
        if (textView != null) {
            textView.setText(textView.getContext().getString(stringId, objects));
        }
    }

    public static void setText(TextView textView, String format, Object... objects) {
        if (textView != null) {
            textView.setText(String.format(Locale.CHINA, format, objects));
        }
    }
    //加载默认原图滤镜效果
    public static void setImage(ImageView imageView, final IFilter filter) {
        if (imageView != null && filter != null && filter.getConfig() != null) {
            String url = getNetImgByName("alter_original");
            if(filter.getConfig().isEmpty()){
                setImage(imageView, url);
                return;
            }
            Glide.with(imageView.getContext())
                    .load(url)
                    .apply(new RequestOptions().transform(new FilterTransform(filter)))
                    .into(imageView);
        }
    }
    public static void setImage(ImageView imageView, String pictures) {
        if (imageView != null) {
            if (TextUtils.isEmpty(pictures)) {
                imageView.setImageResource(R.drawable.media_sheep_logo);
            } else {
                if (pictures.contains(";")) {
                    pictures = pictures.split(";")[0];
                }
                Glide.with(imageView.getContext())
                        .load(pictures)
                        .into(imageView);
            }
        }
    }
    public static String getNetImgByName(String name) {
        return String.format(Locale.CHINA, "http://cdngame.kuaifazs.com/%s.png", name);
    }
    public static String getNetImgByNameAndSuffix(String name) {
        return String.format(Locale.CHINA, "http://cdngame.kuaifazs.com/%s", name);
    }
    //刷新RecyclerView的adapter
    public static void notifyDataSetChanged(RecyclerView recyclerView) {
        if (recyclerView != null && recyclerView.getAdapter() != null)
            recyclerView.getAdapter().notifyDataSetChanged();
    }
}
