package com.usai.redant.CommonEditor.CommonEditor;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;


import com.usai.redant.CommonEditor.ImageUploadActivity;
import com.usai.redant.CommonEditor.ImageViewActivity;
import com.usai.redant.redantmobile.R;

import org.json.JSONObject;


public class CommonEditorImageCellHolder extends CommonEditorCellHolder{

    public TextView title_lb;
    public TextView desc_lb;
    public ImageButton img_btn_0;
    public ImageButton img_btn_1;
    public ImageButton img_btn_2;
    public boolean editable;


    public CommonEditorImageCellHolder(Context context, View view) {
        super(context, view);

        title_lb = (TextView) view.findViewById(R.id.img_cell_title_lb);
        desc_lb = (TextView) view.findViewById(R.id.img_cell_desc_lb);

        img_btn_0 = (ImageButton) view.findViewById(R.id.img_btn_0);
        img_btn_1 = (ImageButton) view.findViewById(R.id.img_btn_1);
        img_btn_2 = (ImageButton) view.findViewById(R.id.img_btn_2);

        img_btn_0.setTag(0);
        img_btn_1.setTag(1);
        img_btn_2.setTag(2);

        img_btn_0.setOnClickListener(clickListener);
        img_btn_1.setOnClickListener(clickListener);
        img_btn_2.setOnClickListener(clickListener);

    }

    private String path0;
    public void setImage0(String path,Bitmap bitmap) {
        img_btn_0.setImageBitmap(bitmap);
        path0 = path;
    }

    private String path1;
    public void setImage1(String path,Bitmap bitmap) {
        img_btn_1.setImageBitmap(bitmap);
        path1 = path;
    }

    private String path2;
    public void setImage2(String path,Bitmap bitmap) {
        img_btn_2.setImageBitmap(bitmap);
        path2 = path;
    }

    @Override
    public void setItem(JSONObject item_json, boolean readonly, View view) {
        super.setItem(item_json, readonly, view);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageButton btn = (ImageButton)v;
            int btn_tag = (int)btn.getTag()/*Integer.valueOf((String)btn.getTag())*/;

            int UPLOAD_IMAGE_CODE = 1;

            if (editable) {
                // 上传Image

                Intent intent = new Intent(mCtx,ImageUploadActivity.class);
                intent.putExtra("group",group);
                intent.putExtra("child",child);
                intent.putExtra("btn",btn_tag);

                ((Activity)mCtx).startActivityForResult(intent,UPLOAD_IMAGE_CODE);

            } else {
                // 查看Image

                Intent intent = new Intent(mCtx,ImageViewActivity.class);
                if (btn_tag == 0) {
                    if (path0 == null || path0.isEmpty()) {
                        intent = null;
                    } else {
                        intent.putExtra("data", path0);
                    }
                }
                if (btn_tag == 1) {
                    if (path1 == null || path1.isEmpty()) {
                        intent = null;
                    } else {
                        intent.putExtra("data", path1);
                    }
                }
                if (btn_tag == 2) {
                    if (path2 == null || path2.isEmpty()) {
                        intent = null;
                    } else {
                        intent.putExtra("data", path2);
                    }
                }


                if (intent != null) {
                    ((Activity)mCtx).startActivity(intent);
                }

            }
        }
    };
}
