package com.gh.common.util;

import android.content.Context;
import android.content.Intent;
import android.widget.LinearLayout;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.gamecenter.ImageViewerActivity;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.core.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by khy on 2016/11/8.
 * <p>
 * 初始化资讯关注-内容图片
 **/
public class ConcernContentUtils {

    public static void addContentPic(Context context, LinearLayout linearLayout, List<String> list,
                                     String entrance, int width) {
        ArrayList<SimpleDraweeView> imageViewList = new ArrayList<>();
        int count = list.size();
        int index = 0;
        for (int i = 0, size = (int) Math.ceil(list.size() / 3.0f); i < size; i++) {
            int type = count % 3;
            LinearLayout ll;
            switch (type) {
                case 0:
                    ll = new LinearLayout(context);
                    ll.setOrientation(LinearLayout.HORIZONTAL);
                    for (int j = 0; j < 3; j++) {
                        SimpleDraweeView draweeView = getImageView(context, list, index, width, 0);
                        imageViewList.add(draweeView);
                        int position = index;
                        draweeView.setOnClickListener(v -> {
                            Intent checkIntent = ImageViewerActivity.getIntent(context, (ArrayList<String>) list, position, imageViewList, entrance);
                            context.startActivity(checkIntent);
                        });
                        ll.addView(draweeView);
                        index += 1;
                    }
                    linearLayout.addView(ll);
                    count -= 3;
                    break;
                case 1:
                    SimpleDraweeView draweeView = getImageView(context, list, index, width, 1);
                    imageViewList.add(draweeView);
                    int position1 = index;
                    draweeView.setOnClickListener(v -> {
                        Intent checkIntent = ImageViewerActivity.getIntent(context, (ArrayList<String>) list, position1, imageViewList, entrance);
                        context.startActivity(checkIntent);
                    });
                    linearLayout.addView(draweeView);
                    count -= 1;
                    index += 1;
                    break;
                case 2:
                    ll = new LinearLayout(context);
                    ll.setOrientation(LinearLayout.HORIZONTAL);
                    for (int j = 0; j < 2; j++) {
                        SimpleDraweeView imageView = getImageView(context, list, index, width, 2);
                        imageViewList.add(imageView);
                        int position2 = index;
                        imageView.setOnClickListener(v -> {
                            Intent checkIntent = ImageViewerActivity.getIntent(context, (ArrayList<String>) list, position2, imageViewList, entrance);
                            context.startActivity(checkIntent);
                        });
                        ll.addView(imageView);
                        index += 1;
                    }
                    linearLayout.addView(ll);
                    count -= 2;
                    break;
            }
        }
    }

    private static SimpleDraweeView getImageView(final Context context,
                                                 final List<String> list,
                                                 final int position,
                                                 int width,
                                                 int type) {
        SimpleDraweeView imageView;
        LinearLayout.LayoutParams lparams;
        switch (type) {
            case 0:
                imageView = new SimpleDraweeView(context);
                lparams = new LinearLayout.LayoutParams(
                        0, width / 3 - DisplayUtils.dip2px(context, 4));
                lparams.setMargins(DisplayUtils.dip2px(context, 2), 0,
                        DisplayUtils.dip2px(context, 2), DisplayUtils.dip2px(context, 4));
                lparams.weight = 1;
                imageView.setLayoutParams(lparams);
                ImageUtils.display(context.getResources(), imageView, width / 3 - DisplayUtils.dip2px(context, 4),
                        ScalingUtils.ScaleType.CENTER_CROP, list.get(position));
                break;
            case 1:
                imageView = new SimpleDraweeView(context);
                lparams = new LinearLayout.LayoutParams(width, width / 2);
                lparams.setMargins(DisplayUtils.dip2px(context, 2), 0,
                        DisplayUtils.dip2px(context, 2), DisplayUtils.dip2px(context, 4));
                imageView.setLayoutParams(lparams);
                ImageUtils.display(context.getResources(), imageView, lparams.width,
                        ScalingUtils.ScaleType.CENTER_CROP, list.get(position));
                break;
            default:
                imageView = new SimpleDraweeView(context);
                lparams = new LinearLayout.LayoutParams(
                        0, width / 2 - DisplayUtils.dip2px(context, 4));
                lparams.setMargins(DisplayUtils.dip2px(context, 2), 0,
                        DisplayUtils.dip2px(context, 2), DisplayUtils.dip2px(context, 4));
                lparams.weight = 1;
                imageView.setLayoutParams(lparams);
                ImageUtils.display(context.getResources(), imageView, width / 2 - DisplayUtils.dip2px(context, 4),
                        ScalingUtils.ScaleType.CENTER_CROP, list.get(position));
                break;
        }
        return imageView;
    }
}
