package com.bbitcn.silk.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bbitcn.silk.adapter.PriceAdapter;
import com.bbitcn.silk.base.BaseActivity;
import com.bbitcn.silk.databinding.ActivityMainBinding;
import com.bbitcn.silk.databinding.ItemPriceBinding;
import com.bbitcn.silk.model.net.TodayPrice;
import com.bbitcn.silk.model.net.Weather;
import com.bbitcn.silk.ui.dialog.ConfirmDialog;
import com.bbitcn.silk.ui.dialog.UpdateDialog;
import com.bbitcn.silk.utils.MMKVUtil;
import com.bbitcn.silk.utils.MyUtil;
import com.bbitcn.silk.utils.global.Global;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainActivityPresenter> {

    @Override
    public void initView() {
        binding.rvPrice.setLayoutManager(new GridLayoutManager(mContext, 2));
        presenter.getNoticeUpdate().observe(this, model -> {
            if (model != null) {
                int server = model.getVersionNumber();
                if (server > MyUtil.getVersion(mContext)) {
                    // 强制升级
                    new UpdateDialog(mContext, model.getUrl(), model.getVersionName(), model.getVersionDescription(), true).show();
                    return;
                }
            }
            successTips("已是最新版本");
            if (!StringUtils.isEmpty(MMKVUtil.get(Global.TOKEN))) {
                //之前登陆过 -> 静默登录（后获取用户信息）
                MyUtil.login(() -> presenter.getRefreshUserInfo());
            } else {
                //之前没登陆过 - > 跳转到登录界面
                ActivityUtils.finishAllActivities(true);
                startActivity(new Intent(mContext, LoginActivity.class));
            }
        });
        presenter.getNoticeTodayPrice().observe(this, todayPrice -> {
            binding.tvPriceDate.setText("价格获取时间：" + todayPrice.getData().getRefreshTime());
            List<TodayPrice.DataDTO.ItemsDTO> items = todayPrice.getData().getItems();
            binding.rvPrice.setAdapter(new PriceAdapter(items, ItemPriceBinding.class));
            binding.tvNoPrice.setVisibility(items.isEmpty() ? View.VISIBLE : View.GONE);
        });
        presenter.getNoticeUserInfo().observe(this, userInfo -> {
            if (userInfo != null) {
                binding.tvUserName.setText(userInfo.getData().getUserName());
                binding.tvCompanyName.setText(userInfo.getData().getDepName());
                binding.tvUserRole.setText(userInfo.getData().getRoleName());
            }
            // 获得用户信息后再获取天气与价格
            presenter.getWeather();
            presenter.getPriceOfToday();
        });
        presenter.getNoticeWeather().observe(this, dataDTO -> {
            List<Weather.DataDTO.CastsDTO> casts = dataDTO.getCasts();
            Weather.DataDTO.CastsDTO castsDTO = casts.get(0);
            binding.tvCity.setText("(" + dataDTO.getCity() + ")");
            binding.tvWeatherDate1.setText(castsDTO.getDate());
            binding.tvWeather1.setText(Math.min(Integer.parseInt(castsDTO.getDaytemp()), Integer.parseInt(castsDTO.getNighttemp()))
                    + "~" + Math.max(Integer.parseInt(castsDTO.getDaytemp()), Integer.parseInt(castsDTO.getNighttemp())) + "℃ "
                    + castsDTO.getDayweather());
            castsDTO = casts.get(1);
            binding.tvWeatherDate2.setText(castsDTO.getDate());
            binding.tvWeather2.setText(Math.min(Integer.parseInt(castsDTO.getDaytemp()), Integer.parseInt(castsDTO.getNighttemp()))
                    + "~" + Math.max(Integer.parseInt(castsDTO.getDaytemp()), Integer.parseInt(castsDTO.getNighttemp())) + "℃ "
                    + castsDTO.getDayweather());
            castsDTO = casts.get(2);
            binding.tvWeatherTop.setText(TimeUtils.getChineseWeek(castsDTO.getDate(), new SimpleDateFormat("yyyy-MM-dd")));
            binding.tvWeatherDate3.setText(castsDTO.getDate());
            binding.tvWeather3.setText(Math.min(Integer.parseInt(castsDTO.getDaytemp()), Integer.parseInt(castsDTO.getNighttemp()))
                    + "~" + Math.max(Integer.parseInt(castsDTO.getDaytemp()), Integer.parseInt(castsDTO.getNighttemp())) + "℃ "
                    + castsDTO.getDayweather());
        });
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.ivUserCenter.setOnClickListener(v -> startActivity(new Intent(mContext, UserActivity.class)));
        binding.ivQuery.setOnClickListener(v -> startActivity(new Intent(mContext, QueryActivity.class)));
        binding.ivPrice.setOnClickListener(v -> startActivity(new Intent(mContext, PriceActivity.class)));
        binding.ivTotal.setOnClickListener(v -> startActivity(new Intent(mContext, TotalActivity.class)));
        binding.ivKinds.setOnClickListener(v -> startActivity(new Intent(mContext, KindsActivity.class)));
        binding.btnRefreshPrice.setOnClickListener(v -> presenter.getPriceOfToday());
    }

    @Override
    public void onBackPressed() {
        new ConfirmDialog(mContext, "提示", "是否退出程序？", () -> AppUtils.exitApp()).show();
    }
}