package com.qiyou.mb.android.ui.fragments;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.R;
import com.qiyou.mb.android.utils.ImageLoader;
import com.qiyou.mb.android.utils.image.*;

import org.json.JSONObject;


/**
 * Created by wangmi on 17/1/5.
 */

public class QCode_fragment extends QiyouFragment {

    public static QCode_fragment newInstance(String json) {

        QCode_fragment f = new QCode_fragment();

        Bundle args = new Bundle();
//	    args.putString(QiYouConstant.EXTRA_KEY_ID, trackId);
//	    args.putString(QiYouConstant.EXTRA_KEY_TYPE, inType);
        args.putString(QiYouConstant.EXTRA_KEY_GENERAL,json );

        f.setArguments(args);

        return f;
    }

    private String qCodeString = "";
    private QimageView mImageView;
    private TextView qdesc ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        activity.app.playBack=false;

        vContainer = inflater.inflate(R.layout.qcode_fragment, container, false);
        super. onCreateView( inflater,  container, savedInstanceState);

        if (getArguments() != null) {
//	        String  Id = getArguments().getString(QiYouConstant.EXTRA_KEY_ID);
//	        if(Id!=null)trackId =Integer.valueOf(Id);
            qCodeString = getArguments().getString(QiYouConstant.EXTRA_KEY_GENERAL);
//	        String t=getArguments().getString(QiYouConstant.EXTRA_KEY_TYPE);
//	        if(!TextUtils.isEmpty(t))
//	        	inType=Integer.valueOf(t);

        }
        mImageView = (QimageView) vContainer.findViewById(R.id.img_qcode);
        qdesc =(TextView)  vContainer.findViewById(R.id.txt_qcode);
        return vContainer;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String txtDesc = "";


        try {
            JSONObject jo = new JSONObject(qCodeString);
            txtDesc = jo.getString(QiYouConstant.EXTRA_KEY_QCODEDESC);

        } catch (Exception e) {

        }


//        Bitmap qrcode = generateQRCode(qCodeString);
//        mImageView.setImageBitmap(qrcode);
        setImage();
        qdesc.setText(txtDesc);


    }



    void setImage(){

        final String filePath = QiYouConstant.SD_PATH
                + "qrcode"  + ".jpg";
       final ImageLoader  IL = new ImageLoader(activity);
        //二维码图片较大时，生成图片、保存文件的时间可能较长，因此放在新线程中


        boolean success = QRCodeUtil.createQRImage(qCodeString, 800, 800,
                null,
                filePath);

        if (success) {
            IL.DisplayImage(filePath, mImageView);
        }

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                boolean success = QRCodeUtil.createQRImage(qCodeString, 800, 800,
//                         null,
//                        filePath);
//
//                if (success) {
//                    IL.DisplayImage(filePath, mImageView);
//                }
//            }
//        }).start();


    }






    private Bitmap generateQRCode(String content) {
        try {
            QRCodeWriter writer = new QRCodeWriter();
            // MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 500, 500);
            return bitMatrix2Bitmap(matrix);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }
    private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
        int w = matrix.getWidth();
        int h = matrix.getHeight();
        int[] rawData = new int[w * h];
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                int color = Color.WHITE;
                if (matrix.get(i, j)) {
                    color = Color.BLACK;
                }
                rawData[i + (j * w)] = color;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
        return bitmap;
    }

    @Override
    public void setFTag(){
        TAG="com.qiyou.QCode_fragment";
    }
    public static String getFTag(){
        return "com.qiyou.QCode_fragment";
    }
    @Override
    public String getCurrentTag(){

        return "com.qiyou.QCode_fragment";

    }

    @Override
    public boolean isShowBottomTabs() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void setTabOnFiling() {

//		 maxTabIndex = 3;

    }

}
