package com.stm.asset.page;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.stm.asset.R;
import com.stm.asset.base.BaseFragmentActivity;
import com.stm.asset.base.ConfigKey;
import com.stm.asset.base.FragPageAdapter;
import com.stm.asset.base.StmApplication;
import com.stm.asset.view.LoadingDialog;

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

/**
 * 类名称：CheckTaskActivity
 * 类描述：盘点任务页面
 * 创建人：story
 * 创建时间：2021-12-07 13:54
 */
public class CheckTaskActivity extends BaseFragmentActivity implements View.OnClickListener {

    private TextView unfinishedTxt, finishedTxt;
    private ViewPager selectVPager;

    private FragPageAdapter fragPageAdapter;
    private List<Fragment> fragmentList;
    private TaskFragment unfinishedTaskFgm;
    private TaskFragment finishedTaskFgm;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_task);
        initView();
    }

    private void initView() {
        initTitle();
        titleTxt.setText("资产盘点");
        findViewById(R.id.common_title_back_layout).setOnClickListener(this);
        unfinishedTxt = findViewById(R.id.task_unfinished_txt);
        finishedTxt = findViewById(R.id.task_finished_txt);
        unfinishedTxt.setText("未盘点资产(0)");
        finishedTxt.setText("已完成资产");
        unfinishedTxt.setSelected(true);
        finishedTxt.setSelected(false);
        unfinishedTxt.setOnClickListener(this);
        finishedTxt.setOnClickListener(this);
        selectVPager = findViewById(R.id.task_content);
        fragmentList = new ArrayList<>();
        unfinishedTaskFgm = TaskFragment.instance(this,"A");
        fragmentList.add(unfinishedTaskFgm);
        fragPageAdapter = new FragPageAdapter(getSupportFragmentManager(), fragmentList);
        selectVPager.setAdapter(fragPageAdapter);
        loadingDialog = new LoadingDialog(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back_layout:
                finish();
                break;
            case R.id.task_unfinished_txt:
                if (!unfinishedTxt.isSelected()) {
                    unfinishedTxt.setSelected(true);
                    finishedTxt.setSelected(false);
                    selectVPager.setCurrentItem(0);
                }
                break;
            case R.id.task_finished_txt:
                if (!finishedTxt.isSelected()) {
                    if (finishedTaskFgm == null) {
                        finishedTaskFgm = TaskFragment.instance(this,"B");
                        fragmentList.add(finishedTaskFgm);
                        fragPageAdapter.notifyDataSetChanged();
                    }
                    unfinishedTxt.setSelected(false);
                    finishedTxt.setSelected(true);
                    selectVPager.setCurrentItem(1);
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case ConfigKey.PERMISSION_REQUEST_EXTERNAL_STORAGE:
                boolean sp = true;
                for (int i=0;i<grantResults.length;i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        sp = false;
                        break;
                    }
                }
                if (sp) {
                    if (unfinishedTaskFgm != null) {
                        unfinishedTaskFgm.selectTaskFile();
                    }
                } else {
                    StmApplication.getInstance().showToastShort("导出数据需要访问手机存储权限");
                }
                break;
        }
    }

    public void setUnfinishedNum(int num) {
        String str = "未盘点资产(" + num + ")";
        unfinishedTxt.setText(str);
    }

    public void setFinishedNum(int num) {
        String str = "已完成资产";
        finishedTxt.setText(str);
    }

    public LoadingDialog getLoadingDialog() {
        return loadingDialog;
    }

    public void refreshData() {
        if (unfinishedTaskFgm != null) {
            unfinishedTaskFgm.loadData();
        }
        if (finishedTaskFgm != null) {
            finishedTaskFgm.loadData();
        }
    }
}
