package com.longtu.sample;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.acegameproject.burstshooter.R;
import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.LTBaseParam;
import com.longtu.sdk.base.Listener.LTBaseGameNoticeListener;
import com.longtu.sdk.base.Listener.LTBaseUpdatePCDNPathListener;
import com.longtu.sdk.base.bean.LTBaseDeviceInfo;
import com.longtu.sdk.base.view.LTTip;
import com.longtu.sdk.utils.Log.Logs;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * SDK Demo 登录控件，模拟游戏登录和切换账号功能，调用登录和切换账号接口
 *
 * @author 2017年9月20日 上午11:12:56
 */

public class LTBase_Demo_MainView extends LinearLayout implements OnClickListener {

    private Context mContext;
    private Button mButton_login;
    private Button mButton_loginswittch;
    private Button mButton_getnotice;
    private Button mButton_getcdnview;
    private Button mButton_switchLanguage;
    private Button mButton_DevicesInfo;
//    private Button mButton_getfloatview;
//    private Button mButton_closefloatview;

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

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.longtu_demo_login, this);
        this.mButton_login = (Button) this.findViewById(R.id.button_login);
        this.mButton_login.setOnClickListener(this);
        this.mButton_loginswittch = (Button) this.findViewById(R.id.button_swittch);
        this.mButton_loginswittch.setOnClickListener(this);
        this.mButton_getnotice = (Button) this.findViewById(R.id.button_getnotice);
        this.mButton_getnotice.setOnClickListener(this);
        this.mButton_getcdnview = (Button) this.findViewById(R.id.button_getcdnview);
        this.mButton_getcdnview.setOnClickListener(this);

        this.mButton_switchLanguage = this.findViewById(R.id.button_switchLanguage);
        this.mButton_switchLanguage.setOnClickListener(this);
        this.mButton_DevicesInfo = (Button) this.findViewById(R.id.button_getdeviceinfo);
        this.mButton_DevicesInfo.setOnClickListener(this);
//
//        this.mButton_closefloatview = (Button) this.findViewById(R.id.button_closefloatview);
//        this.mButton_closefloatview.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        if (view == this.mButton_login) {
            if (LTBase_Demo_MainActivity.isInitSuccess) {
                // 调用登录接口，游戏需要调用(SDK接口)
                LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKShowLoginView();
            } else {
                Logs.fi(LTBase_Demo_MainActivity.LOGTAG, " 初始化未完成，不允许点击登录 ");
            }


        } else if (view == this.mButton_loginswittch) {
            // 调用切换账号接口，游戏必须调用(SDK接口)
            if (LTBase_Demo_MainActivity.isInitSuccess) {
                if (LTBase_Demo_MainActivity.canswitchaccount) {
                    LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKSwitchAccount();
                } else {
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "当前渠道SDK不需要切换账号功能", Toast.LENGTH_SHORT).show();
                }
            } else {
                Logs.fi(LTBase_Demo_MainActivity.LOGTAG, " 初始化未完成，不允许点击登录 ");
            }

        } else if (view == this.mButton_getnotice) {
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKGetGameNotice(LTBaseParam.GAME_NOTICE_TYPE_LOGIN,
                    mLTBaseGameNoticeListener);
        } else if (view == this.mButton_getcdnview) {
            // 获取CDN地址
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKgetUpdateCDNPath(new LTBaseUpdatePCDNPathListener() {

                @Override
                public void getPathSuccess(JSONObject cdnPath) {

                    Logs.fi("LTBaseSDKLog", " cdnPathtoString:" + cdnPath.toString() + " cdnPath:" + cdnPath);
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity,
                            "获取CDN地址成功 cdnPath = " + cdnPath.toString(),
                            Toast.LENGTH_SHORT).show();
                }

                @Override
                public void getPathFail(int code) {
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "获取CDN地址失败 code = " + code, Toast.LENGTH_SHORT).show();
                }
            });
//
        } else if (view == this.mButton_switchLanguage) {
            switchLocale();

        } else if (view == this.mButton_DevicesInfo) {
            showDevciesDialog();
        }
    }

    //设备信息弹窗
    private void showDevciesDialog() {
        LTBaseDeviceInfo mLTBaseDeviceInfo = LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseGetCurrentDeviceInfo();

        String brand = mLTBaseDeviceInfo.getDeviceBrand();
        String model = mLTBaseDeviceInfo.getDeviceModel();
        String ScreenName = mLTBaseDeviceInfo.getDeviceScreenName();
        int ScreenType = mLTBaseDeviceInfo.getDeviceScreenType();
        int Width, Height;
        Height = mLTBaseDeviceInfo.getHeightPixels();
        Width = mLTBaseDeviceInfo.getWidthPixels();

        StringBuffer DeviceInfoSb = new StringBuffer();
        DeviceInfoSb.append("品牌:" + brand + " \n ");
        DeviceInfoSb.append("型号:" + model + " \n ");
        DeviceInfoSb.append("屏幕类型名:" + ScreenName + " \n ");
        DeviceInfoSb.append("屏幕类型:" + ScreenType + " \n ");
        DeviceInfoSb.append("屏幕Width:" + Height + " \n ");
        DeviceInfoSb.append("屏幕高:" + Width + " \n ");

        LTTip mLTTip = new LTTip(LTBase_Demo_MainActivity.mMainActivity, true, "确定", "取消",
                DeviceInfoSb.toString(), new LTTip.OnTipClickListener() {

            @Override
            public void onClickConfirm() {

            }

            @Override
            public void onClickCancel() {

            }
        });

        mLTTip.show();
    }


    /**
     * 切换语言
     */
    private void switchLocale() {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                LTBase_Demo_MainActivity.mMainActivity);
        builder.setIcon(R.drawable.longtu_sample_ic_launcher);
        builder.setTitle("选择语言");
        // 指定下拉列表的显示数据
        final String[] cities = {"马来西亚", "菲律宾", "泰国", "印尼", "繁体", "中国", "英文"};
        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_MY);
                        break;
                    case 1:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_PH);
                        break;
                    case 2:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_TH);
                        break;
                    case 3:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_IND);
                        break;
                    case 4:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_TRADITIONAL_CHINESE);
                        break;
                    case 5:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_CHINESE);
                        break;
                    case 6:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_English);
                        break;
                    default:
                        break;
                }
            }
        });
        builder.show();
    }


    /**
     * 游戏内公告数据解析
     */
    private LTBaseGameNoticeListener mLTBaseGameNoticeListener = new LTBaseGameNoticeListener() {

        @Override
        public void getNoticeSuccess(int code, int dataSize, String data) {
            Log.i("ltbase_demo", " getNoticeSuccess code = " + code + " dataSize = " + dataSize + " data = " + data);
            if (dataSize == 0) {
                Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "未配置公告信息", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    JSONArray dataJson = new JSONArray(data);
                    for (int i = 0; i < dataJson.length(); i++) {
                        JSONObject noticeJson = dataJson.getJSONObject(i);
                        String noticeId = noticeJson.getString("noticeId");
                        String noticeType = noticeJson.getString("noticeType");
                        String noticeTitle = noticeJson.getString("noticeTitle");
                        String noticeContent = noticeJson.getString("noticeContent");
                        String contentType = noticeJson.getString("contentType");
                    }

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

            }
        }

        @Override
        public void getNoticeFail(int code) {
            Log.i("ltbase_demo", " getNoticeFail code = " + code);

        }
    };


//    public static String GetPhoneInfo() {
//        // String cpu = "";
//        JSONObject json = new JSONObject();
//        try {
//            String str1 = "/proc/cpuinfo";
//            // String[] cpuInfo = {"",""};
//            FileReader fr = new FileReader(str1);
//            BufferedReader localBufferReader = new BufferedReader(fr, 8192);
//            String line = null;
//            String hardware1 = "";
//            while ((line = localBufferReader.readLine()) != null) {
//
//                if (line.toLowerCase().indexOf("hardware") != -1) {
//                    hardware1 = line.substring(line.toLowerCase().indexOf("hardware") + "hardware".length() + 3);
//                    // json.putOpt("hardware1", line);
//                    break;
//                }
//            }
//            json.putOpt("hardware1", hardware1);
//            json.putOpt("hardware2", Build.HARDWARE);
//            localBufferReader.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        try {
//            json.putOpt("BOARD", Build.BOARD);
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                json.putOpt("SUPPORTED_ABIS", Arrays.toString(Build.SUPPORTED_ABIS));
//            } else {
//                json.putOpt("SUPPORTED_ABIS", "");
//            }
//
//            json.putOpt("DEVICE", Build.DEVICE);
//            json.putOpt("DISPLAY", Build.DISPLAY);
//            json.putOpt("FINGERPRINT", Build.FINGERPRINT);
//            json.putOpt("SERIAL", Build.SERIAL);
//            json.putOpt("ID", Build.ID);
//            json.putOpt("MANUFACTURER", Build.MANUFACTURER);
//            json.putOpt("PRODUCT", Build.PRODUCT);
//            json.putOpt("TAGS", Build.TAGS);
//            json.putOpt("TYPE", Build.TYPE);
//            json.putOpt("VERSIONINCREMENTAL", Build.VERSION.INCREMENTAL);
//            json.putOpt("VERSIONRELEASE", Build.VERSION.RELEASE);
//            json.putOpt("VERSIONSDK_INT", Build.VERSION.SDK_INT);
//            json.putOpt("BOOTLOADER", Build.BOOTLOADER);
//            json.putOpt("PRODUCT", Build.PRODUCT);
//            json.putOpt("MODEL", Build.MODEL);
//            json.putOpt("TIME", Build.TIME);
//            json.putOpt("osname", System.getProperty("os.name"));
//            json.putOpt("osarch", System.getProperty("os.arch"));
//            Log.e("Unity", "GetCPU  " + json.toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return json.toString();
//    }


//    /**
//     * 获取当前应用程序的包名
//     * @param context 上下文对象
//     * @return 返回包名
//     */
//    public static String getAppProcessName(Context context) {
//        try
//        {
//            //当前应用pid
//            int pid = android.os.Process.myPid();
//            //任务管理类
//            ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//            //遍历所有应用
//            List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
//            for (ActivityManager.RunningAppProcessInfo info : infos) {
//                if (info.pid == pid)//得到当前应用
//                    return info.processName;//返回包名
//            }
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//        return "";
//    }


}
