package com.kfzs.duanduan;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kfzs.appstore.utils.restful.KFZSNetwork;
import com.kfzs.duanduan.event.BigEvent;
import com.kfzs.duanduan.react.TabsHelper;
import com.kfzs.duanduan.utils.StatusBarUtils;
import com.kfzs.duanduan.utils.dlg.HelperUtils;
import com.kfzs.duanduan.view.DialogStorageLow;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.Container;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.R;
import com.umeng.socialize.UMShareAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;


/**
 *
 */
public class ActMain extends BaseActivity implements TabsHelper.ItemPosition {
    private static final String TAG = "ActMain";

    @BindView(R.id.other_container)
    LinearLayout other_container;

    public final static String INTENT_GAME_ID = "INTEN_GAME_ID";


    private ImageView mImgDot;//下载按钮的小红点


    public TabsHelper tabsHelper;

    private TabsHelper.ItemPosition itemPosition;

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

    @Override
    public void initView() {
        KFZSApp.actMain = this;
        tabsHelper = new TabsHelper(this);
        HelperUtils.init();//初始化屏幕的高，density等
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        mImgDot = findViewById(R.id.img_download_manager_dot);
        tabsHelper.init();

    }

    @Override
    public void initListener() {
        findViewById(R.id.edt_main_search_enter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ActMain.this, ActSearch.class));
            }
        });
        findViewById(R.id.img_download_manager).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImgDot.setVisibility(View.GONE);
                startActivity(new Intent(ActMain.this, ActDownloadMgr.class));
                UMConfigUtils.onEvent(UMConfigUtils.Event.GAME_DOWNLOAD_MANAGER);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusSC();
    }

    /**
     * 设置状态栏沉浸式
     */
    private void setStatusSC() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION	 | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    /**
     * 设置状态栏正常
     */
    private void setStatusVI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#29d6fd"));
           //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }



    private Container<Action1<Integer>> container = new Container<>();

    public Container<Action1<Integer>> getContainer() {
        return container;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (container != null && container.getT() != null)
            container.getT().call(1);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (container != null && container.getT() != null)
            container.getT().call(2);
    }

    /**
     * 传递事件
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        KFZSNetwork.stopByTag(TAG);
        EventBus.getDefault().unregister(this);
        if (container != null && container.getT() != null)
            container.setT(null);
    }


    /**
     * 主要的事件广播接收更新处理
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(BigEvent event) {
        switch (event.getEventTypes()) {
            case TIPS_DOWN_ICON:
                mImgDot.setVisibility(View.VISIBLE);
                break;
            case STORAGE_LOW:
                DialogStorageLow.showDialog(this);
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (intent.hasExtra(INTENT_GAME_ID)) {
            tabsHelper.goToPostion(intent.getIntExtra(INTENT_GAME_ID, 0));
        } else {
            recreate();
        }
    }

    /**
     *
     */
    @Override
    public void onBackPressed() {
        tabsHelper.onBackPressed();
    }

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

    @Override
    public void positionClick(int position) {
        switch (position) {
            case 0://赚钱
                setStatusSC();
                break;
            /*case 1://审核
                setStatusVI();
                break;
            case 2://邀请
                setStatusVI();
                break;
            case 3://发现
                setStatusVI();
                break;
            case 4://个人中心
                setStatusVI();
                break;*/
            default:
                setStatusVI();
                break;

        }
    }
}
