package cn.yyxx.commsdk.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import java.util.List;

import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.base.utils.Logger;
import cn.yyxx.support.AppUtils;
import cn.yyxx.support.ResUtils;
import cn.yyxx.support.permission.IPermissionCallback;
import cn.yyxx.support.permission.Permission;
import cn.yyxx.support.permission.PermissionKit;

public class YYXXWelcomeActivity extends Activity {


    private static final int CODE_GO_INIT = 1000;
    private static final int CODE_GO_GAME_ACTIVITY = 1001;

    private LinearLayout layout;
    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CODE_GO_INIT:
                    goInit();
                    break;
                case CODE_GO_GAME_ACTIVITY:
                    goGameActivity();
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setView();
        YYXXCommSdk.getInstance().initAgreement(this, new ICallback() {
            @Override
            public void onResult(int code, String result) {
                Logger.logHandler("result : " + result + "\n");

                if (code == 0) {
                    if (layout == null){
                        handler.sendEmptyMessage(CODE_GO_GAME_ACTIVITY);
                    }else{
                        handler.sendEmptyMessageDelayed(CODE_GO_INIT, 400);
                    }
                } else {
                    System.exit(0);
                }
            }
        });
    }

    private void goInit() {
        Animation animation = new AlphaAnimation(0.3f, 1.0f);
        animation.setDuration(2000);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {

                handler.sendEmptyMessage(CODE_GO_GAME_ACTIVITY);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        layout.startAnimation(animation);

    }


    private void setView() {
        int id = 0;

        id = ResUtils.getResId(this, "yyxx_comm_welcome", "drawable");

        if (id == 0) {

            return;
        }

        layout = new LinearLayout(this);
        layout.setBackgroundResource(id);
        setContentView(layout);
    }

    private void goGameActivity() {
        String action = getPackageName();
        Logger.i("goGameActivity -> action = " + action);
        startActivity(new Intent(action));
        finish();
    }

}
