package com.longtu.sample;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.DisplayCutout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowInsets;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bh.sdk.Interface.LTBaseSDK;
import com.bh.sdk.LTGameUserData;
import com.longtu.sdk.base.LTBaseConstant;
import com.longtu.sdk.base.LTBaseParam;
import com.longtu.sdk.base.Listener.LTBaseGameNoticeListener;
import com.longtu.sdk.base.Listener.LTBaseSDKListener;
import com.longtu.sdk.base.Listener.LTBaseSDKPushListener;
import com.longtu.sdk.base.Listener.LTBaseUpdatePCDNPathListener;
import com.longtu.sdk.base.nativeNotice.LTBaseNativeNoticeManage;
import com.longtu.sdk.base.nativeNotice.LTBaseNativeNoticeView;
import com.longtu.sdk.utils.Log.Logs;
import com.longtugame.globalsample.R;


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

import java.util.List;

/**
 * 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_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_getfloatview = (Button) this.findViewById(R.id.button_getfloatview);
//        this.mButton_getfloatview.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) {
            // 调用登录接口，游戏需要调用(SDK接口)
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKShowLoginView();

        } else if (view == this.mButton_loginswittch) {
            // 调用切换账号接口，游戏必须调用(SDK接口)
            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 if (view == this.mButton_getnotice) {
//            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKGetGameNotice(LTBaseParam.GAME_NOTICE_TYPE_LOGIN,
//                    mLTBaseGameNoticeListener);

            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity)
                    .LTPushSetPushListener(mLTBaseSDKPushListener);
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTPushGetPushToken();
        } else if (view == this.mButton_getcdnview) {
            // 获取CDN地址
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKgetUpdateCDNPath(new LTBaseUpdatePCDNPathListener() {

                @Override
                public void getPathSuccess(JSONObject cdnPath) {
                    Toast.makeText(LTBase_Demo_MainActivity.mMainActivity,
                            "获取CDN地址成功 cdnPath = " + cdnPath,
                            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();

        }
    }


    /**
     * 获取推送token
     */
    private LTBaseSDKPushListener mLTBaseSDKPushListener = new LTBaseSDKPushListener() {
        @Override
        public void LTBaseSDKPushToken(int code, String token) {
            Logs.i(LTBase_Demo_MainActivity.LOGTAG, "获取push  token code: " + code + "  token: " + token);
        }
    };

    /**
     * 切换语言
     */
    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_ID);
                        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;
                    case 7:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_DE);
                        break;
                    case 8:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_RU);
                        break;
                    case 9:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_FR);
                        break;
                    case 10:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_ES);
                        break;
                    case 11:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_IT);
                        break;
                    case 12:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_PT);
                        break;
                    case 13:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_JA);
                        break;
                    case 14:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_KO);
                        break;
                    case 15:
                        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).
                                LTBaseUpdateLocale(LTBaseParam.LOCALE_TR);
                        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);

        }
    };


}
