package com.uiui.zyappstore.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.uiui.zyappstore.R;
import com.uiui.zyappstore.adapter.AppAdapter;
import com.uiui.zyappstore.base.BaseActivity;
import com.uiui.zyappstore.bean.AppInfo;
import com.uiui.zyappstore.bean.BaseResponse;
import com.uiui.zyappstore.common.CommonDatas;
import com.uiui.zyappstore.network.NetInterfaceManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

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;

    private AppAdapter mAppAdapter;


    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_class_app;
    }

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

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }
        String className = intent.getStringExtra(CommonDatas.APP_CLASS_NAME);
        if (!TextUtils.isEmpty(className)) {
            mMoreappTvTitle.setText(className);
        }
        int classId = intent.getIntExtra(CommonDatas.APP_CLASS_ID, 0);
        mAppAdapter = new AppAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mAppAdapter);
        getAppByClass(classId);
    }

    @Override
    protected void setListener() {

    }

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

    public void getAppByClass(int classId) {
        NetInterfaceManager.getInstance().getAdminAppByClassObservable(classId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<List<AppInfo>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAppByClass", "onSubscribe: ");
                    }

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

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAppByClass", "onError: " + e.getMessage());
                        setAllAppError();
                        onComplete();
                    }

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

    private void setAllAppdata(BaseResponse<List<AppInfo>> listBaseResponse) {
        int code = listBaseResponse.code;
        if (code == 200) {
            List<AppInfo> 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);
    }
}
