package com.longtu.sample;

import java.util.HashMap;
import java.util.UUID;

import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.LTBaseDataCollector;
import com.longtu.sdk.utils.Log.Logs;
import com.longtugame.globalsample.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 统计相关功能和接口使用
 */
public class LTBase_Demo_Analyticslayout extends LinearLayout implements OnClickListener {

    private Context mContext;
    private Button mRegistration, mLogin, mdeeplink, mPurchase, mcreaterole, mDiy1;
    private Button mTutorialCompletion, mFirstPurchase, mSecondPurchase;

    private Button mad_MainMenu;

    public LTBase_Demo_Analyticslayout(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
        mContext = context;
        init();
    }

    public LTBase_Demo_Analyticslayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.longtu_demo_anlyticsdialog, this);

        mRegistration = (Button) v.findViewById(R.id.registration);
        mRegistration.setOnClickListener(this);
        mLogin = (Button) v.findViewById(R.id.login);
        mLogin.setOnClickListener(this);
        mdeeplink = (Button) v.findViewById(R.id.tutorial_deeplink);
        mdeeplink.setOnClickListener(this);
        mPurchase = (Button) v.findViewById(R.id.purchase);
        mPurchase.setOnClickListener(this);
        mcreaterole = (Button) v.findViewById(R.id.createrole);
        mcreaterole.setOnClickListener(this);
        mDiy1 = (Button) v.findViewById(R.id.diy1);
        mDiy1.setOnClickListener(this);

        mTutorialCompletion = (Button) v.findViewById(R.id.af_tutorial_completion);
        mTutorialCompletion.setOnClickListener(this);
        mFirstPurchase = (Button) v.findViewById(R.id.af_first_purchase);
        mFirstPurchase.setOnClickListener(this);
        mSecondPurchase = (Button) v.findViewById(R.id.af_second_purchase);
        mSecondPurchase.setOnClickListener(this);

        mad_MainMenu = v.findViewById(R.id.ad_MainMenu);
        mad_MainMenu.setOnClickListener(this);

    }

    @SuppressLint("UseValueOf")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == mRegistration) {
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            logValue.put("userId", LTBase_Demo_MainActivity.LONGTU_UserId);
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTBaseSDKSendAnalyticsInfoLog("registration", logValue);
        } else if (v == mLogin) {
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            logValue.put("userId", LTBase_Demo_MainActivity.LONGTU_UserId);
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTBaseSDKSendAnalyticsInfoLog("login",
                            logValue);

        } else if (v == mdeeplink) {
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            logValue.put("link", "linklinklinklink");
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTBaseSDKSendAnalyticsInfoLog("ReceiveDeepLink", logValue);

        } else if (v == mPurchase) {
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            // Double price = new Double("108.01");
            logValue.put("price", "100");
            logValue.put("userId", LTBase_Demo_MainActivity.LONGTU_UserId);
            logValue.put("orderId", UUID.randomUUID());
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTBaseSDKSendAnalyticsInfoLog("purchase",
                            logValue);

        } else if (v == mcreaterole) {
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            logValue.put("roleId", LTBase_Demo_MainActivity.LONGTU_UserId);
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTBaseSDKSendAnalyticsInfoLog("CreateRole", logValue);

        } else if (v == mDiy1) {
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTBaseSDKSendAnalyticsInfoLog("CustEvent1", logValue);
        } else if (v == mTutorialCompletion) {
            // 完成新手引导
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            LTBaseSDK.getInstance(LTBaseDataCollector.getInstance().getmActivity())
                    .LTBaseSDKSendAnalyticsInfoLog("tutorial_complete",
                            logValue);
        } else if (v == mFirstPurchase) {

            //发送第一次计费
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            logValue.put("price", 300);
            logValue.put("userId", "testuserid");
            logValue.put("orderId", "testorderid");
            LTBaseSDK.getInstance(LTBaseDataCollector.getInstance().getmActivity())
                    .LTBaseSDKSendAnalyticsInfoLog("firstPurchase",
                            logValue);

        } else if (v == mSecondPurchase) {

            //发送第二次计费
            HashMap<String, Object> logValue = new HashMap<String, Object>();
            logValue.put("price", "3.00");
            logValue.put("userId", "testuserid");
            logValue.put("orderId", "testorderid");
            LTBaseSDK.getInstance(LTBaseDataCollector.getInstance().getmActivity())
                    .LTBaseSDKSendAnalyticsInfoLog("secondPurchase",
                            logValue);

        } else if (v == mad_MainMenu) {
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseToTabJoyAd("Main Menu");
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        Logs.i("ltbasedemo", "onKeyDown  keyCode = " + keyCode + " event getRepeatCount = " + event.getRepeatCount());
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // (SDK接口)
            LTBase_Demo_MainActivity.mMainActivity.setContentView(LTBase_Demo_MainActivity.mPayView);
            LTBase_Demo_MainActivity.mPayView.mAnalyticslayout = null;
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
