package com.bc_chat.im.messages.gif;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bc_chat.im.R;
import com.bc_chat.im.activity.GifPicturePreviewActivity;
import com.bumptech.glide.Glide;

import io.rong.imkit.R.drawable;
import io.rong.imkit.R.id;
import io.rong.imkit.R.string;
import io.rong.imkit.RongIM;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.utilities.PermissionCheckUtil;
import io.rong.imkit.utilities.RongUtils;
import io.rong.imkit.widget.AsyncImageView;
import io.rong.imkit.widget.CircleProgressView;
import io.rong.imkit.widget.provider.IContainerItemProvider.MessageProvider;
import io.rong.imlib.IRongCallback.IDownloadMediaMessageCallback;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.Message.MessageDirection;
import io.rong.imlib.model.Message.SentStatus;

@ProviderTag(
        messageContent = GIFMessage.class,
        showProgress = false,
        showReadState = true
)
public class GIFMessageItemProvider extends MessageProvider<GIFMessage> {
    private static final String TAG = "GIFMessageItemProvider";

    public GIFMessageItemProvider() {
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_gif_message, (ViewGroup) null);
        ViewHolder holder = new ViewHolder();
        holder.img = (AsyncImageView) view.findViewById(id.rc_img);
        holder.preProgress = (ProgressBar) view.findViewById(R.id.rc_pre_progress);
        holder.loadingProgress = (CircleProgressView) view.findViewById(R.id.rc_gif_progress);
        holder.startDownLoad = (ImageView) view.findViewById(R.id.rc_start_download);
        holder.downLoadFailed = (ImageView) view.findViewById(R.id.rc_download_failed);
        holder.length = (TextView) view.findViewById(R.id.rc_length);
        holder.fireView = (FrameLayout) view.findViewById(R.id.rc_destruct_click);
        holder.sendFire = (FrameLayout) view.findViewById(R.id.fl_send_fire);
        holder.receiverFire = (FrameLayout) view.findViewById(R.id.fl_receiver_fire);
        holder.receiverFireImg = (ImageView) view.findViewById(R.id.iv_receiver_fire);
        holder.receiverFireText = (TextView) view.findViewById(R.id.tv_receiver_fire);
        holder.clickHint = (TextView) view.findViewById(R.id.rc_destruct_click_hint);
        view.setTag(holder);
        return view;
    }

    public void onItemClick(View view, int position, GIFMessage content, UIMessage message) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder.startDownLoad.getVisibility() == View.VISIBLE) {
            holder.startDownLoad.setVisibility(View.GONE);
            if (this.checkPermission(view.getContext())) {
                this.downLoad(message.getMessage(), holder);
            } else {
                holder.downLoadFailed.setVisibility(View.VISIBLE);
                holder.length.setVisibility(View.VISIBLE);
                holder.length.setText(this.formatSize(content.getGifDataSize()));
                Toast.makeText(view.getContext(), string.rc_ac_file_download_request_permission, Toast.LENGTH_SHORT).show();
            }
        } else if (holder.downLoadFailed.getVisibility() == View.VISIBLE) {
            holder.downLoadFailed.setVisibility(View.GONE);
            if (this.checkPermission(view.getContext())) {
                this.downLoad(message.getMessage(), holder);
            } else {
                holder.downLoadFailed.setVisibility(View.VISIBLE);
                holder.length.setVisibility(View.VISIBLE);
                holder.length.setText(this.formatSize(content.getGifDataSize()));
                Toast.makeText(view.getContext(), string.rc_ac_file_download_request_permission, Toast.LENGTH_SHORT).show();
            }
        } else if (holder.preProgress.getVisibility() != View.VISIBLE && holder.loadingProgress.getVisibility() != View.VISIBLE && content != null) {
            Intent intent = new Intent(view.getContext(), GifPicturePreviewActivity.class);
            intent.setPackage(view.getContext().getPackageName());
            intent.putExtra("message", content);
            view.getContext().startActivity(intent);
        }

    }

    public void bindView(View v, int position, GIFMessage content, UIMessage message) {
        ViewHolder holder = (ViewHolder) v.getTag();
        holder.receiverFire.setTag(message.getUId());
        holder.startDownLoad.setVisibility(View.GONE);
        holder.downLoadFailed.setVisibility(View.GONE);
        holder.preProgress.setVisibility(View.GONE);
        holder.loadingProgress.setVisibility(View.GONE);
        holder.length.setVisibility(View.GONE);
//        int[] paramsValue = this.getParamsValue(v.getContext(), content.getWidth(), content.getHeight());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(content.getWidth(), content.getHeight());
        holder.img.setLayoutParams(params);
        holder.img.setImageDrawable(v.getContext().getResources().getDrawable(drawable.def_gif_bg));
        int progress = message.getProgress();
        if (message.getMessageDirection() == MessageDirection.SEND) {
            SentStatus status = message.getSentStatus();
            if (progress > 0 && progress < 100) {
                holder.loadingProgress.setProgress(progress, true);
                holder.loadingProgress.setVisibility(View.VISIBLE);
                holder.preProgress.setVisibility(View.GONE);
            } else if (status.equals(SentStatus.SENDING)) {
                holder.loadingProgress.setVisibility(View.GONE);
            } else if (progress == -1) {
                holder.loadingProgress.setVisibility(View.GONE);
                holder.preProgress.setVisibility(View.GONE);
                holder.downLoadFailed.setVisibility(View.VISIBLE);
                holder.length.setVisibility(View.VISIBLE);
            } else {
                holder.loadingProgress.setVisibility(View.GONE);
                holder.preProgress.setVisibility(View.GONE);
            }
        } else if (message.getReceivedStatus().isDownload()) {
            if (progress > 0 && progress < 100) {
                holder.loadingProgress.setProgress(progress, true);
                holder.loadingProgress.setVisibility(View.VISIBLE);
                holder.preProgress.setVisibility(View.GONE);
                holder.startDownLoad.setVisibility(View.GONE);
            } else if (progress == 100) {
                holder.loadingProgress.setVisibility(View.GONE);
                holder.preProgress.setVisibility(View.GONE);
                holder.length.setVisibility(View.GONE);
                holder.startDownLoad.setVisibility(View.GONE);
            } else if (progress == -1) {
                holder.loadingProgress.setVisibility(View.GONE);
                holder.preProgress.setVisibility(View.GONE);
                holder.downLoadFailed.setVisibility(View.VISIBLE);
                holder.length.setVisibility(View.VISIBLE);
                holder.startDownLoad.setVisibility(View.GONE);
            } else {
                holder.loadingProgress.setVisibility(View.GONE);
                holder.preProgress.setVisibility(View.VISIBLE);
                holder.length.setVisibility(View.VISIBLE);
                holder.startDownLoad.setVisibility(View.GONE);
            }
        } else {
            holder.loadingProgress.setVisibility(View.GONE);
            holder.preProgress.setVisibility(View.GONE);
            holder.length.setVisibility(View.GONE);
            holder.startDownLoad.setVisibility(View.GONE);
            if (progress == -1) {
                holder.downLoadFailed.setVisibility(View.VISIBLE);
                holder.length.setVisibility(View.VISIBLE);
                holder.length.setText(this.formatSize(content.getGifDataSize()));
            }
        }

        if (content.isDestruct()) {
            Drawable drawable;
            if (message.getMessageDirection() == MessageDirection.SEND) {
                holder.sendFire.setVisibility(View.VISIBLE);
                holder.receiverFire.setVisibility(View.GONE);
                holder.fireView.setBackgroundResource(R.drawable.rc_ic_bubble_no_right);
                drawable = v.getContext().getResources().getDrawable(R.drawable.rc_fire_sender_album);
                drawable.setBounds(0, 0, RongUtils.dip2px(40.0F), RongUtils.dip2px(34.0F));
                holder.clickHint.setCompoundDrawables((Drawable) null, drawable, (Drawable) null, (Drawable) null);
                holder.clickHint.setTextColor(Color.parseColor("#FFFFFF"));
            } else {
                holder.sendFire.setVisibility(View.GONE);
                holder.receiverFire.setVisibility(View.VISIBLE);
                holder.fireView.setBackgroundResource(R.drawable.rc_ic_bubble_no_left);
                drawable = v.getContext().getResources().getDrawable(R.drawable.rc_fire_receiver_album);
                drawable.setBounds(0, 0, RongUtils.dip2px(40.0F), RongUtils.dip2px(34.0F));
                holder.clickHint.setCompoundDrawables((Drawable) null, drawable, (Drawable) null, (Drawable) null);
                holder.clickHint.setTextColor(Color.parseColor("#F4B50B"));
//                DestructManager.getInstance().addListener(message.getUId(), new GIFMessageItemProvider.DestructListener(holder, message), "GIFMessageItemProvider");
//                if (message.getMessage().getReadTime() > 0L) {
//                    holder.receiverFireText.setVisibility(View.VISIBLE);
//                    holder.receiverFireImg.setVisibility(View.GONE);
//                    String unFinishTime;
//                    if (TextUtils.isEmpty(message.getUnDestructTime())) {
//                        unFinishTime = DestructManager.getInstance().getUnFinishTime(message.getUId());
//                    } else {
//                        unFinishTime = message.getUnDestructTime();
//                    }

//                    holder.receiverFireText.setText(unFinishTime);
//                    holder.receiverFireText.setVisibility(View.GONE);
//                    DestructManager.getInstance().startDestruct(message.getMessage());
//                } else {
//                    holder.receiverFireText.setVisibility(View.GONE);
//                    holder.receiverFireImg.setVisibility(View.VISIBLE);
//                }
                holder.receiverFireText.setVisibility(View.GONE);
                holder.receiverFireImg.setVisibility(View.GONE);
            }
        } else {
            holder.receiverFire.setVisibility(View.GONE);
            holder.sendFire.setVisibility(View.GONE);
        }

        if (content.getLocalPath() != null) {
            if (content.isDestruct()) {
                holder.fireView.setVisibility(View.VISIBLE);
                holder.img.setVisibility(View.GONE);
            } else {
                holder.fireView.setVisibility(View.GONE);
                holder.img.setVisibility(View.VISIBLE);
                this.loadGif(v, content.getLocalUri(), holder);
            }
        } else {
//            int size = v.getContext().getResources().getInteger(integer.rc_gifmsg_auto_download_size);
            int size = 1024;
            if (content.getGifDataSize() <= (long) (size * 1024)) {
                if (this.checkPermission(v.getContext())) {
                    if (!message.getReceivedStatus().isDownload()) {
                        message.getReceivedStatus().setDownload();
                        this.downLoad(message.getMessage(), holder);
                    }
                } else if (progress != -1) {
                    holder.startDownLoad.setVisibility(View.VISIBLE);
                    holder.length.setVisibility(View.VISIBLE);
                    holder.length.setText(this.formatSize(content.getGifDataSize()));
                }
            } else if (progress > 0 && progress < 100) {
                holder.startDownLoad.setVisibility(View.GONE);
                holder.length.setVisibility(View.VISIBLE);
                holder.length.setText(this.formatSize(content.getGifDataSize()));
            } else if (progress != -1) {
                holder.startDownLoad.setVisibility(View.VISIBLE);
                holder.preProgress.setVisibility(View.GONE);
                holder.loadingProgress.setVisibility(View.GONE);
                holder.downLoadFailed.setVisibility(View.GONE);
                holder.length.setVisibility(View.VISIBLE);
                holder.length.setText(this.formatSize(content.getGifDataSize()));
            }
        }

    }

    public Spannable getContentSummary(GIFMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, GIFMessage data) {
        return new SpannableString(context.getString(string.rc_message_content_gif));
    }

    private void downLoad(Message downloadMsg, ViewHolder holder) {
        holder.preProgress.setVisibility(View.VISIBLE);
        RongIM.getInstance().downloadMediaMessage(downloadMsg, (IDownloadMediaMessageCallback) null);
    }

    private void loadGif(View v, Uri uri, ViewHolder holder) {
//        ((RequestBuilder)Glide.with(v.getContext()).asGif().diskCacheStrategy(DiskCacheStrategy.RESOURCE)).load(uri.getPath()).listener(new RequestListener<GifDrawable>() {
//            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
//                return false;
//            }
//
//            public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
//                return false;
//            }
//        }).into(holder.img);
        Glide.with(v.getContext()).load(uri.getPath()).into(holder.img);

    }

    private String formatSize(long length) {
        float size;
        if (length > 1048576L) {
            size = (float) Math.round((float) length / 1048576.0F * 100.0F) / 100.0F;
            return size + "M";
        } else if (length > 1024L) {
            size = (float) Math.round((float) length / 1024.0F * 100.0F) / 100.0F;
            return size + "KB";
        } else {
            return length + "B";
        }
    }

    private int[] getParamsValue(Context context, int width, int height) {
        int maxWidth = dip2px(context, 120.0F);
        int minValue = dip2px(context, 80.0F);
        int finalWidth;
        int finalHeight;
        float scale;
        if (width > maxWidth) {
            finalWidth = maxWidth;
            scale = (float) width / (float) maxWidth;
            finalHeight = Math.round((float) height / scale);
            if (finalHeight < minValue) {
                finalHeight = minValue;
            }
        } else if (width < minValue) {
            finalWidth = minValue;
            scale = (float) width / (float) minValue;
            finalHeight = Math.round((float) height * scale);
            if (finalHeight < minValue) {
                finalHeight = minValue;
            }
        } else {
            finalWidth = Math.round((float) height);
            finalHeight = Math.round((float) width);
        }

        int[] params = new int[]{finalWidth, finalHeight};
        return params;
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5F);
    }

    private boolean checkPermission(Context context) {
        String[] permission = new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"};
        return PermissionCheckUtil.checkPermissions(context, permission);
    }

    public void onItemLongClick(View view, int position, GIFMessage content, UIMessage message) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder.startDownLoad.getVisibility() != View.VISIBLE && holder.downLoadFailed.getVisibility() != View.VISIBLE && holder.loadingProgress.getVisibility() != View.VISIBLE) {
            super.onItemLongClick(view, position, content, message);
        }
    }

    private static class ViewHolder {
        AsyncImageView img;
        ProgressBar preProgress;
        CircleProgressView loadingProgress;
        ImageView startDownLoad;
        ImageView downLoadFailed;
        TextView length;
        FrameLayout fireView;
        FrameLayout sendFire;
        FrameLayout receiverFire;
        ImageView receiverFireImg;
        TextView receiverFireText;
        TextView clickHint;

        private ViewHolder() {
        }
    }
}
