package com.uiuipad.appstore.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.uiuipad.appstore.R;
import com.uiuipad.appstore.adapter.AppAdapter;
import com.uiuipad.appstore.base.BaseActivity;
import com.uiuipad.appstore.bean.AdminAppInfo;
import com.uiuipad.appstore.bean.AppInfo;
import com.uiuipad.appstore.base.BaseResponse;
import com.uiuipad.appstore.common.CommonConfig;
import com.uiuipad.appstore.network.NetInterfaceManager;
import com.uiuipad.appstore.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class ClassAppActivity extends BaseActivity {

    @BindView(R.id.moreapp_tv_title)
    TextView mMoreappTvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    @BindView(R.id.iv_search)
    ImageView iv_search;

    private AppAdapter mAppAdapter;


    /**
     * 设置布局
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_class_app;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }
        String className = intent.getStringExtra(CommonConfig.APP_CLASS_NAME);
        if (!TextUtils.isEmpty(className)) {
            mMoreappTvTitle.setText(className);
        }
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show("功能暂未开放");
            }
        });
        int classId = intent.getIntExtra(CommonConfig.APP_CLASS_ID, 0);
        mAppAdapter = new AppAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mAppAdapter);
        getAppByClass(classId);
    }

    public void finish(View view) {
        finish();
    }

    public void getAppByClass(int classId) {
        NetInterfaceManager.getInstance().getAdminClassAppObservable(classId)
                .compose(RxLifecycle.bindUntilEvent(lifecycleSubject, ActivityEvent.DESTROY))
                .subscribe(new Observer<BaseResponse<List<AdminAppInfo>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAppByClass", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<AdminAppInfo>> listBaseResponse) {
                        Log.e("getAppByClass", "onSubscribe: ");
                        setAllAppdata(listBaseResponse);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAppByClass", "onSubscribe: ");
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAppByClass", "onSubscribe: ");
                    }
                });

    }

    private void setAllAppdata(BaseResponse<List<AdminAppInfo>> listBaseResponse) {
        int code = listBaseResponse.code;
        if (code == 200) {
            List<AdminAppInfo> infos = listBaseResponse.data;
            mRecyclerView.setVisibility(View.VISIBLE);
            iv_nodata.setVisibility(View.GONE);
            mAppAdapter.setData(infos);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            iv_nodata.setImageDrawable(getDrawable(R.drawable.app_nodata));
            iv_nodata.setVisibility(View.VISIBLE);
        }
    }

    private void setAllAppError() {
        mRecyclerView.setVisibility(View.GONE);
        iv_nodata.setImageDrawable(getDrawable(R.drawable.app_nonetwork));
        iv_nodata.setVisibility(View.VISIBLE);
    }
}
