package com.qiyou.mb.android.ui.fragments;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.zxing.Result;
import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.R;
import com.qiyou.mb.android.utils.server.RestMethods;

import org.json.JSONObject;

import me.dm7.barcodescanner.zxing.ZXingScannerView;
/**
 * Created by wangmi on 17/1/5.
 */

public class Scan_fragment extends WebList_fragment implements ZXingScannerView.ResultHandler {

    private ZXingScannerView mScannerView;

    public static Scan_fragment newInstance() {

        Scan_fragment f = new Scan_fragment();
        return f;
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        activity.updateActionBarTitle(R.string.frg_notes);

        mScannerView = new ZXingScannerView(activity);   // Programmatically initialize the scanner view
//        setContentView(mScannerView);                // Set the scanner view as the content view
        vContainer = mScannerView;
        return vContainer;
    }


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

        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume

    }




    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v(TAG, rawResult.getText()); // Prints scan results
        Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

        String result = rawResult.getText();

        String rest = "";
        int svrId = 0;
        JSONObject jo = new JSONObject();
        try {
             jo = new JSONObject(result);
            rest = jo.getString(QiYouConstant.EXTRA_KEY_RESTMETHOD);
            svrId = jo.getInt(QiYouConstant.EXTRA_KEY_SVRID);
        } catch (Exception e) {

        }

        if (rest.equalsIgnoreCase(RestMethods.ACT_P_ADD_MEMBER) || rest.equalsIgnoreCase(RestMethods.GRP_P_ADD_MEMBER)){


            String svrName ="";
            int ownerId = 0;
            try {

                svrName  = jo.getString("svrName");
                ownerId = jo.getInt("ownerId");

            }catch (Exception e) {

            }


            doMemberOperation(svrId,svrName,ownerId,activity.app.user.getUserbean().getId(),activity.app.user.getUserbean().getUserName(),rest);

            if (rest.equalsIgnoreCase(RestMethods.ACT_P_ADD_MEMBER) ) {
                 requestActFromWeb(svrId);
            }else  if (rest.equalsIgnoreCase(RestMethods.GRP_P_ADD_MEMBER) ) {
                 requestGroupFromWeb(svrId);
            }
        }




        mScannerView.stopCamera();

    }
    @Override
    protected void initialCommonControlls(){}

    @Override
    public void doQuery(){};


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

         return "com.qiyou.Scan_fragment";

    }

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

    @Override
    public void setTabOnFiling() {

//		 maxTabIndex = 3;

    }











}



