package com.sipai.jspatrol.Main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

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

import com.sipai.jspatrol.Base.ActivityCollector;
import com.sipai.jspatrol.Main.fragment.faultfragment.FaultFragment;
import com.sipai.jspatrol.Main.fragment.fifthFragment.FifthFragment;
import com.sipai.jspatrol.Main.fragment.mainfragment.MainFragment;
import com.sipai.jspatrol.Main.fragment.promFragment.ProMFragment;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.Service.IPatrolService;
import com.sipai.jspatrol.background.locationservice.LocationService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FrameLayout fl;
    private RelativeLayout rl_main,rl_fault,rl_pro,rl_self;
    private ImageView iv_main,iv_fault,iv_pro,iv_self;
    private TextView tv_main,tv_fault,tv_pro,tv_self;

    public static void actionStart(Context context){
        Intent inent = new Intent(context,MainActivity.class);
        context.startActivity(inent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCollector.addActivity(this);
        initView();
        startPatrolService();
        replaceFragment(new MainFragment());
    }

    private void startPatrolService() {
        Intent intent = new Intent(MainActivity.this, IPatrolService.class);
        startService(intent);
        Intent intent_b = new Intent(MainActivity.this, LocationService.class);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android8.0以上通过startForegroundService启动service
            startForegroundService(intent_b);
        }else{
            startService(intent_b);
        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        fl = findViewById(R.id.moudle_main_fl);
        rl_main = findViewById(R.id.moudle_main_activity_rl_main);
        rl_main.setOnClickListener(this);
        rl_fault = findViewById(R.id.moudle_main_activity_rl_falut);
        rl_fault.setOnClickListener(this);
        rl_pro = findViewById(R.id.moudle_main_activity_rl_pro);
        rl_pro.setOnClickListener(this);
        rl_self = findViewById(R.id.moudle_main_activity_rl_self);
        rl_self.setOnClickListener(this);
        iv_main = findViewById(R.id.moudle_main_activity_iv_main);
        iv_fault = findViewById(R.id.moudle_main_activity_iv_falut);
        iv_pro = findViewById(R.id.moudle_main_activity_iv_pro);
        iv_self = findViewById(R.id.moudle_main_activity_iv_self);
        tv_main = findViewById(R.id.moudle_main_activity_tv_main);
        tv_fault = findViewById(R.id.moudle_main_activity_tv_falut);
        tv_pro = findViewById(R.id.moudle_main_activity_tv_pro);
        tv_self = findViewById(R.id.moudle_main_activity_tv_self);
        changeTab(tv_main);
        iv_main.setImageResource(R.mipmap.moudle_tab_main_full);
    }

    private void changeTab(TextView tv) {
        iv_main.setImageResource(R.mipmap.moudle_tab_main_empty);
        iv_fault.setImageResource(R.mipmap.moudle_tab_fault_empty);
        iv_pro.setImageResource(R.mipmap.moudle_tab_pro_empty);
        iv_self.setImageResource(R.mipmap.moudle_tab_self_empty);
        tv_main.setTextColor(getResources().getColor(R.color.tab_text_gray));
        tv_fault.setTextColor(getResources().getColor(R.color.tab_text_gray));
        tv_pro.setTextColor(getResources().getColor(R.color.tab_text_gray));
        tv_self.setTextColor(getResources().getColor(R.color.tab_text_gray));
        tv.setTextColor(getResources().getColor(R.color.black));
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.moudle_main_fl,fragment);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.moudle_main_activity_rl_main:
                replaceFragment(new MainFragment());
                changeTab(tv_main);
                iv_main.setImageResource(R.mipmap.moudle_tab_main_full);
                break;
            case R.id.moudle_main_activity_rl_falut:
                replaceFragment(new FaultFragment());
                changeTab(tv_fault);
                iv_fault.setImageResource(R.mipmap.moudle_tab_fault_full);
                break;
            case R.id.moudle_main_activity_rl_pro:
                replaceFragment(new ProMFragment());
                changeTab(tv_pro);
                iv_pro.setImageResource(R.mipmap.moudle_tab_pro_full);
                break;
            case R.id.moudle_main_activity_rl_self:
                replaceFragment(new FifthFragment());
                changeTab(tv_self);
                iv_self.setImageResource(R.mipmap.moudle_tab_self_full);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
        Intent intent = new Intent(MainActivity.this, IPatrolService.class);
        stopService(intent);
        Intent intent_b = new Intent(MainActivity.this, LocationService.class);
        stopService(intent_b);
    }
}
