package com.sheep.gamegroup.view.activity;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by ljy on 2018/3/9.
 */

public class SplashAct extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    @BindView(R.id.btn_join)
    TextView btnJoin;
    private Activity activity;

    private boolean isClick;

    private static final int PERMISSON_REQUESTCODE = 1;
    /**
     * 需要进行检测的权限数组
     */
    protected String[] needPermissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //恢复成默认主题
        setTheme(R.style.FullScreenTheme);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestCodePermissions();
    }

    @AfterPermissionGranted(PERMISSON_REQUESTCODE)
    private void requestCodePermissions() {
        if (!EasyPermissions.hasPermissions(this, needPermissions)) {
            EasyPermissions.requestPermissions(this, "应用需要这些权限", PERMISSON_REQUESTCODE, needPermissions);
        } else {
//            setContentView(R.layout.splash_activity);
//            if(Build.VERSION.SDK_INT >= 21){
//                animWelcomeImage();
//
//            }else {
//            }
            mHandler.sendEmptyMessageDelayed(0, BuildConfig.DEBUG ? 100 : 1000);
        }
    }

    Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(!isClick){
                isClick = true;
                goMain();
            }

        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.splash_activity;
    }

    @Override
    public void initView() {
        activity = this;
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onPermissionsGranted(int i, @NonNull List<String> list) {
        recreate();
    }

    @Override
    public void onPermissionsDenied(int i, @NonNull List<String> list) {
        showMissingPermissionDialog();
    }

    private void showMissingPermissionDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("当前应用缺少必要权限。请点击\"设置\"-\"权限\"-打开所需权限。");

        // 拒绝, 退出应用
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });

        builder.setPositiveButton("设置",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startAppSettings();
                    }
                });

        builder.setCancelable(false);

        builder.show();
    }

    /**
     * 启动应用的设置
     */
    private void startAppSettings() {
        Intent intent = new Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }

    private synchronized void goMain() {
        String openId = SpUtils.getOpenId(getApplicationContext());
        if (!TextUtils.isEmpty(openId)) {
            Intent intent = new Intent(activity, com.kfzs.duanduan.ActMain.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(activity, LoginAct.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick(R.id.btn_join)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.btn_join:
                if(!isClick){
                    isClick = true;
                    goMain();
                }
                break;
        }
    }

    @Override
    public void onActivityReenter(int resultCode, Intent data) {
        super.onActivityReenter(resultCode, data);
        try{
            mHandler.sendEmptyMessageDelayed(0, 300);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try{
            mHandler.removeMessages(0);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
