package com.uiui.zyappstore.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.arialyy.aria.core.Aria;
import com.uiui.zyappstore.BuildConfig;
import com.uiui.zyappstore.R;
import com.uiui.zyappstore.activity.DownloadManagerActivity;
import com.uiui.zyappstore.activity.LocalManagerActivity;
import com.uiui.zyappstore.adapter.UpdateAppAdapter;
import com.uiui.zyappstore.base.LazyLoadFragment;
import com.uiui.zyappstore.bean.AppInfo;
import com.uiui.zyappstore.bean.LocalApp;
import com.uiui.zyappstore.bean.UpdateAppInfo;
import com.uiui.zyappstore.utils.ApkUtils;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asus on 2017/10/23.
 * 管理的页面
 */

public class ManageFragment extends LazyLoadFragment {

    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    @BindView(R.id.manage_tv_appNum)
    TextView manage_tv_appNum;
    @BindView(R.id.manage_tv_taskNum)
    TextView manage_tv_taskNum;
    @BindView(R.id.manage_tv_updateNum)
    TextView manage_tv_updateNum;
    @BindView(R.id.tv_version)
    TextView tv_version;
    @BindView(R.id.manage_cl_localapp)
    ConstraintLayout manage_cl_localapp;
    @BindView(R.id.manage_cl_download)
    ConstraintLayout manage_cl_download;
    @BindView(R.id.manage_rv_local)
    RecyclerView mRvLocal;

    private List<LocalApp> localAppList;
    private List<UpdateAppInfo> updateAppInfoList;
    private List<AppInfo> applist;
    private List<String> packageNameList = new ArrayList<>();
    private UpdateAppAdapter adapter;
    private Context mContext;
//    private TextView tv_pripolicy;
//    private TextView tv_useragreement;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_manage;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
//        tv_pripolicy = view.findViewById(R.id.tv_pripolicy);
//        tv_useragreement = view.findViewById(R.id.tv_useragreement);
//        tv_pripolicy.setOnClickListener(v -> {
//            startActivity(new Intent(getActivity(), PrivacyPolicyActivity.class));
//        });
//        tv_useragreement.setOnClickListener(v -> {
//            startActivity(new Intent(getActivity(), UserAgreementActivity.class));
//        });
        tv_version.setText("版本 " + BuildConfig.VERSION_NAME);
        if (Aria.download(this).getAllNotCompleteTask() != null) {
            manage_tv_taskNum.setText(String.valueOf(Aria.download(this).getAllNotCompleteTask().size()));
        } else {
            manage_tv_taskNum.setText("0");
        }
        manage_cl_localapp.setOnClickListener(v -> startActivity(new Intent(mContext, LocalManagerActivity.class)));
        manage_cl_download.setOnClickListener(v -> startActivity(new Intent(mContext, DownloadManagerActivity.class)));
        updateAppInfoList = new ArrayList<>();
        adapter = new UpdateAppAdapter(updateAppInfoList, mContext);
        mRvLocal.setAdapter(adapter);
        mRvLocal.setLayoutManager(new LinearLayoutManager(mContext));
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void loadData(Bundle savedInstanceState) {

    }

    @Override
    public void onStart() {
        super.onStart();
        mContext = getActivity();
        List<LocalApp> localApps = ApkUtils.getLocalApp(getActivity());
        if (localApps != null && localApps.size() > 0) {
            manage_tv_appNum.setText(localApps.size() + "");
        }
    }
}
