package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.zxing.Result;
import com.sheep.gamegroup.model.entity.ScreenshotsEntity;
import com.sheep.gamegroup.model.entity.TaskChild;
import com.sheep.gamegroup.model.entity.TaskDescEntity;
import com.sheep.gamegroup.model.entity.Release_task;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.MyListview;
import com.sheep.gamegroup.util.QRUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPreview;
import rx.functions.Action1;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.TASK_SAVE_QR;

/**
 * Created by ljy on 2018/6/14.
 */

public class TaskdetailSonListviewAdp extends BaseAdapter {
    private List<TaskChild> descEntityArrayList;
    private Context context;
    private Release_task taskReleaseEty;
    int with;

    public TaskdetailSonListviewAdp(List<TaskChild> descEntityArrayList, Context context) {
        this.descEntityArrayList = descEntityArrayList;
        this.context = context;
        with = G.WIDTH > G.HEIGHT ? G.HEIGHT / 3 - 50 : G.WIDTH / 3 - 50;
    }

    public void addTaskEntity(Release_task taskReleaseEty) {
        this.taskReleaseEty = taskReleaseEty;
    }

    @Override
    public int getCount() {
        return descEntityArrayList == null ? 0 : descEntityArrayList.size();
    }

    @Override
    public TaskChild getItem(int position) {
        return descEntityArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Object tag = convertView == null ? null : convertView.getTag();
        ViewHolder holder;
        if (tag instanceof ViewHolder) {
            holder = (ViewHolder) tag;
        } else {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.taskdetail_mylistview_item, null);
            holder.title_item_tv = convertView.findViewById(R.id.title_item_tv);
            holder.num_item_textview = convertView.findViewById(R.id.num_item_textview);
            holder.item_remaining_time_tv = convertView.findViewById(R.id.item_remaining_time_tv);
            holder.select_tab_iv = convertView.findViewById(R.id.select_tab_iv);

            holder.title_item_layout = convertView.findViewById(R.id.title_item_layout);
            holder.taskdetail_screenshot_layout = convertView.findViewById(R.id.taskdetail_screenshot_layout);
            holder.recycler_screenshot_view = convertView.findViewById(R.id.recycler_screenshot_view);
            holder.item_detail_listview = convertView.findViewById(R.id.item_detail_listview);
            holder.item_show_layout = convertView.findViewById(R.id.item_show_layout);
            holder.title_item_price_tv = convertView.findViewById(R.id.title_item_price_tv);
            holder.item_detail_show_layout = convertView.findViewById(R.id.item_detail_show_layout);

            holder.ar_code_layout = convertView.findViewById(R.id.ar_code_layout);
            holder.qr_code_iv = convertView.findViewById(R.id.qr_code_iv);
            holder.qr_code_notice_tv = convertView.findViewById(R.id.qr_code_notice_tv);
            holder.btn_task_item = convertView.findViewById(R.id.btn_task_item);

            convertView.setTag(holder);
        }
        final TaskChild descEntity = descEntityArrayList.get(position);
        if (descEntity == null)
            return convertView;
        if (TextUtils.isEmpty(descEntity.getAmount())) {
            holder.title_item_layout.setVisibility(View.GONE);
            holder.item_show_layout.setPadding(10, 3, 3, 3);
//            holder.item_detail_show_layout.setBackgroundResource(R.drawable.x_shap_shadow_bg_rectgangle_white);
        } else {
            holder.title_item_layout.setVisibility(View.VISIBLE);
            holder.num_item_textview.setText(position + 1 + "");
            CommonUtil.getInstance()
                    .bindDataTaskDetailSon(descEntity, holder.title_item_tv, position);
            holder.title_item_price_tv.setText("+" + descEntity.getAmount() + "元");
            if (descEntity.isSelectFlag()) {
                holder.select_tab_iv.setRotation(180);
                holder.item_show_layout.setVisibility(View.VISIBLE);
                holder.title_item_layout.setBackgroundResource(R.drawable.sp_bg_blue_top);
            } else {
                holder.select_tab_iv.setRotation(0);
                holder.item_show_layout.setVisibility(View.GONE);
                holder.title_item_layout.setBackgroundResource(R.drawable.sp_bg_blue_91bdd6_radius);
            }
            holder.item_detail_show_layout.setBackgroundResource(R.drawable.sp_bg_white_solid_bottom);
        }
        //task_type 1006
        qrCodeView(holder.ar_code_layout, holder.btn_task_item, holder.qr_code_notice_tv, holder.qr_code_iv);


        if (descEntity.getScreenshotsList() != null && descEntity.getScreenshotsList().size() > 0) {
            holder.taskdetail_screenshot_layout.setVisibility(View.VISIBLE);
        } else {
            holder.taskdetail_screenshot_layout.setVisibility(View.GONE);
        }

        /*
          任务详情
          2.6不显示
         */
        holder.item_remaining_time_tv.setVisibility(View.GONE);
//        if(descEntity.getDemo_time() > 0){
//            CommonUtil.getInstance().getHashMap(holder.item_remaining_time_tv);
//
//            holder.item_remaining_time_tv.setVisibility(View.VISIBLE);
//            SelfCountDownTimer selfCountDownTimer = CommonUtil.getInstance().getCountDownTimer(descEntity.getDemo_time(), holder.item_remaining_time_tv, "剩余试玩时间：");
//            selfCountDownTimer.reset(descEntity.getDemo_time());
//            selfCountDownTimer.start();
//
//            CommonUtil.getInstance().addTimerHashMap(holder.item_remaining_time_tv, selfCountDownTimer);
//        }else {
//            holder.item_remaining_time_tv.setVisibility(View.GONE);
//        }

        /*

         */
        if (descEntity.getDescEntityList() != null && descEntity.getDescEntityList().size() > 0) {
            holder.item_detail_listview.setAdapter(new ArrayAdapter<TaskDescEntity>(context, R.layout.textview_layout, descEntity.getDescEntityList()) {
                @Override
                public boolean convert(int position, View convertView, ViewGroup parent, TaskDescEntity item) {
                    TaskDescEntity taskDescEntity = item;
                    if (taskDescEntity == null) {
                        return true;
                    }
                    TextView textView = convertView.findViewById(R.id.textview);
                    CommonUtil.getInstance()
                            .bindDataTaskDetailSonStep(taskDescEntity, textView, position);
                    return true;
                }

                @Override
                public boolean areAllItemsEnabled() {
                    return false;
                }
            });
        }

        /*
         * 截图
         */
        if (!ListUtil.isEmpty(descEntity.getScreenshotsList())) {
            final ArrayList<String> imgListStr = new ArrayList<>();
            for (ScreenshotsEntity s : descEntity.getScreenshotsList()) {
                imgListStr.add(s.getImg());
            }

            LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            holder.recycler_screenshot_view.setHasFixedSize(true);
            holder.recycler_screenshot_view.setLayoutManager(layoutManager);
            holder.recycler_screenshot_view.setAdapter(new AdbCommonRecycler<ScreenshotsEntity>(context, descEntity.getScreenshotsList()) {
                @Override
                public int getViewIdByType(int type) {
                    return R.layout.taskdetail_desc_item_screens;
                }

                @Override
                public void convert(final com.sheep.gamegroup.util.ViewHolder viewHolder, ScreenshotsEntity item) {
                    ImageView screens_iv = viewHolder.itemView.findViewById(R.id.screens_iv);
                    TextView screens_tv = viewHolder.itemView.findViewById(R.id.screens_tv);

                    if (TextUtils.isEmpty(item.getDesc())) {
                        screens_tv.setVisibility(View.GONE);
                    } else {
                        screens_tv.setVisibility(View.VISIBLE);
                        screens_tv.setText(item.getDesc());
                        screens_tv.setMaxWidth(with);
                    }


                    Glide.with(context)
                            .load(item.getImg())
                            .into(screens_iv);
                    screens_iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                if (!ListUtil.isEmpty(imgListStr)) {
                                    PhotoPreview.builder()
                                            .setPhotos(imgListStr)
                                            .setCurrentItem(viewHolder.getAdapterPosition())
                                            .setShowDeleteButton(false)
                                            .start((Activity) context, 101);
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }

            });
        }


        holder.title_item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                descEntity.setSelectFlag(!descEntity.isSelectFlag());
//                if(position == 0){
//                    descEntityArrayList.get(position).setSelectFlag(true);
//                    finalHolder.select_tab_iv.setRotation(180);
//                    finalHolder.item_detail_listview.setVisibility(View.VISIBLE);
//                }else {
//                    descEntityArrayList.get(position).setSelectFlag(false);
//                    finalHolder.select_tab_iv.setRotation(0);
//                    finalHolder.item_detail_listview.setVisibility(View.GONE);
//                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView title_item_tv, item_remaining_time_tv, title_item_price_tv, num_item_textview, btn_task_item, qr_code_notice_tv;
        ImageView select_tab_iv, qr_code_iv;
        LinearLayout title_item_layout, taskdetail_screenshot_layout, item_show_layout, item_detail_show_layout;
        MyListview item_detail_listview;
        RecyclerView recycler_screenshot_view;
        ConstraintLayout ar_code_layout;
    }

    /**
     * 1006 小程序 item 显示
     */
    private void qrCodeView(View ar_code_layout, final View btn_task_item, View qr_code_notice_tv, final View qr_code_iv) {
        //task_type 1006
        if (taskReleaseEty == null || taskReleaseEty.getTask() == null || !taskReleaseEty.getTask().isApplet()) {
            ar_code_layout.setVisibility(View.GONE);
        } else {
            if (taskReleaseEty.isIs_running()) {
                final String name = String.valueOf(taskReleaseEty.getTask().getQr_code().hashCode());
                GlideImageLoader.downloadAndRoundedImage(name, taskReleaseEty.getTask().getQr_code(), (ImageView) qr_code_iv,
                        context.getResources().getDimensionPixelSize(R.dimen.content_padding_150), context.getResources().getDimensionPixelSize(R.dimen.content_padding_8));
                btn_task_item.setVisibility(View.VISIBLE);
                qr_code_notice_tv.setVisibility(View.GONE);
                qr_code_iv.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        GlideImageLoader.downLoadImage(name, taskReleaseEty.getTask().getQr_code(), new Action1<File>() {
                            @Override
                            public void call(File file) {
                                QRUtil.readFile(file, new Action1<Result>() {
                                    @Override
                                    public void call(Result result) {
                                        if (result != null && !TextUtils.isEmpty(result.getText())) {
                                            Jump2View.getInstance().goWeb(context, result.getText());
                                        } else {
                                            G.showToast("请通过微信等应用进行扫描");
                                        }
                                    }
                                });
                            }
                        });
                        return true;
                    }
                });
                btn_task_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TASK_SAVE_QR.onEvent();
                        GlideImageLoader.downLoadImage(name, taskReleaseEty.getTask().getQr_code(), new Action1<File>() {
                            @Override
                            public void call(File file) {
                                if (file != null) {
                                    G.showToast("保存成功，图片路径：" + file.getAbsolutePath());
                                }
                            }
                        });
                    }
                });
            } else {
                btn_task_item.setVisibility(View.GONE);
                qr_code_notice_tv.setVisibility(View.VISIBLE);
            }
            ar_code_layout.setVisibility(View.VISIBLE);
        }
    }
}
