package com.common.activity;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.bean.ProductBean;
import com.common.widget.PenetrateFrameLayout;
import com.common.widget.TopBar;
import com.facebook.accountkit.AccountKitError;
import com.facebook.accountkit.AccountKitLoginResult;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import tech.starwin.LibConfig;
import tech.starwin.R;
import tech.starwin.impl.OnEventClickListener;
import tech.starwin.mvp.ui.adapter.ReviewProductAdapter;
import tech.starwin.utils.LoginManager;
import tech.starwin.utils.context_utils.ActivityJumper;
import tech.starwin.utils.ui_utils.UIHelper;

/**
 * Created by SiKang on 2019-07-26.
 */
public class ReviewHookActivity extends BaseActivity {
    RecyclerView mProcuctRv;

    List<ProductBean> productList;
    ReviewProductAdapter productAdapter;

    TextView mUserNameTv;
    DrawerLayout mDrawer;
    TopBar mTopBar;
    PenetrateFrameLayout fragmentLayout;
    RelativeLayout mMenuLayout;
    ImageButton mBackBtn;


    public static void start(Context context) {
        new ActivityJumper.Builder(context, ReviewHookActivity.class).build().start();
    }


    @Override
    public int bindLayout() {
        return R.layout.activity_review_hook;
    }

    @Override
    public boolean useTopBar() {
        return false;
    }

    @Override
    public void initView() {

        mUserNameTv = findViewById(R.id.activity_review_userName_tv);
        mDrawer = findViewById(R.id.activity_review_drawer_layout);
        mTopBar = findViewById(R.id.activity_review_topbar);
        mMenuLayout = findViewById(R.id.activity_review_menu_layout);
        mBackBtn = findViewById(R.id.activity_review_menu_btn);
        fragmentLayout = findViewById(R.id.activity_review_fragmentLayout);


        QMUIStatusBarHelper.translucent(this);
        mTopBar.setStatusBarHeight(QMUIStatusBarHelper.getStatusbarHeight(this));
        mTopBar.toolBar().addLeftBackImageButton().setVisibility(View.INVISIBLE);
        mTopBar.toolBar().setTitleGravity(Gravity.LEFT);
        mTopBar.toolBar().setBackgroundColor(getResources().getColor(R.color.translateColor));
        mTopBar.toolBar().setTitle(LibConfig.APP_NAME);




        initListener();

        //假数据
        mProcuctRv = findViewById(R.id.activity_review_productList_rv);

        productList = new ArrayList<>();
        int len = new Random().nextInt(5) + 1;
        for (int i = 1; i < len+1; i++) {
            productList.add(new ProductBean(i));
        }

        productAdapter = new ReviewProductAdapter(this, productList);
        UIHelper.bindAdapter(mProcuctRv, productAdapter);


        productAdapter.setOnItemClickListener((itemView, pos, productBean) -> {
            startLogin();
        });
    }


    public void startLogin() {
        LoginManager.get().startFacebookSmsLogin(this, new LoginManager.OnFacebookLoginResult() {
            @Override
            public void onLoginSuccess(AccountKitLoginResult loginResult) {

            }

            @Override
            public void onLoginError(AccountKitError error) {

            }

            @Override
            public void onLoginCancelled() {

            }
        });
    }

    public void initListener() {
        fragmentLayout.setPartnerGroup(mDrawer);
        //Drawer动作监听
        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                if (slideOffset > 0) {
                    fragmentLayout.setClickable(false);
                } else {
                    fragmentLayout.setClickable(true);
                }
                mTopBar.setAlpha(slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                fragmentLayout.setClickable(false);
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                fragmentLayout.setClickable(true);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });


        UIHelper.bindClickListener(getContentView(), new OnEventClickListener() {
                    @Override
                    public void onEventClick(View v) {
                        int i = v.getId();
                        if (i == R.id.activity_review_menu_btn) {
                            if (mDrawer.isDrawerOpen(Gravity.LEFT)) {
                                mDrawer.closeDrawer(Gravity.LEFT);
                            } else {
                                mDrawer.openDrawer(Gravity.LEFT);
                            }
                            return;
                        } else if (i == R.id.activity_review_activity_btn) {
                            ActivityCenter.start(ReviewHookActivity.this);
                            return;
                        } else if (i == R.id.activity_review_helpCenter_btn) {
                            HelpCenterActivity.start(ReviewHookActivity.this);
                            return;
                        } else if (i == R.id.activity_review_aboutUs_btn) {
                            AboutUsActivity.start(ReviewHookActivity.this);
                            return;
                        }
                        //需要登录才能使用的功能
                        if (LoginManager.get().getTokenInfo() == null) {
                            startLogin();
                            return;
                        }
                    }
                }, R.id.activity_review_menu_btn,
                R.id.activity_review_certify_btn,
                R.id.activity_review_myloan_btn,
                R.id.activity_review_activity_btn,
                R.id.activity_review_notification_btn,
                R.id.activity_review_helpCenter_btn,
                R.id.activity_review_aboutUs_btn,
                R.id.activity_review_hotline_btn,
                R.id.activity_review_onlineQA_btn,
                R.id.activity_review_setting_btn);
    }


}
