<?php

namespace Modules\AdministrativeUnits\Controllers;

use App\Core\ApiController;

use Modules\AdministrativeUnits\Models\DistrictModel;
use Modules\AdministrativeUnits\Models\ConstituencyModel;
use Modules\AdministrativeUnits\Models\VillageModel;
use Modules\AdministrativeUnits\Models\CountyModel;
use Modules\AdministrativeUnits\Models\SubcountyModel;
use Modules\AdministrativeUnits\Models\ParishModel;

class Api extends ApiController
{
    protected $districtModel;
    protected $constituencyModel;
    protected $villageModel;
    protected $countyModel;
    protected $subcountyModel;
    protected $parishModel;
  
    public function __construct()
    {
        parent::__construct();
        $this->districtModel = new DistrictModel();
        $this->constituencyModel = new ConstituencyModel();
        $this->villageModel = new VillageModel();
        $this->countyModel = new CountyModel();
        $this->subcountyModel = new SubcountyModel();
        $this->parishModel = new ParishModel();
    }

    public function getDistrictList()
    {
        $start = $this->request->getVar("start");
        $length = $this->request->getVar("length");
        $end = false;
        $data = $this->districtModel->findAll($length,$start);

        $recordsTotal = $this->districtModel->countAll();
       
        $nextLength = (int)$length;
        $nextStart = ((int)$this->request->getVar("start")) + ((int)$length)+1;
        if($nextStart > $recordsTotal)
        {
            $nextLength = (int)$recordsTotal - (int)$start;
            $nextStart = (int)$start;
            $end = true;
        }


        $response = [
            'start' => $nextStart,
            'length' => $nextLength,
            'end' => $end,
            'total' => $recordsTotal,
            'data' => $data,
        ];
		
        return $this->respond($response);
    }

    public function getCountyList()
    {
        $start = $this->request->getVar("start");
        $length = $this->request->getVar("length");
        $end = false;
        $data = $this->countyModel->findAll($length,$start);

        $recordsTotal = $this->countyModel->countAll();
       
        $nextLength = (int)$length;
        $nextStart = ((int)$this->request->getVar("start")) + ((int)$length)+1;
        if($nextStart > $recordsTotal)
        {
            $nextLength = (int)$recordsTotal - (int)$start;
            $nextStart = (int)$start;
            $end = true;
        }


        $response = [
            'start' => $nextStart,
            'length' => $nextLength,
            'end' => $end,
            'total' => $recordsTotal,
            'data' => $data,
        ];
		
        return $this->respond($response);
    }

    public function getSubCountyList()
    {
        $start = $this->request->getVar("start");
        $length = $this->request->getVar("length");
        $end = false;
        $data = $this->subcountyModel->findAll($length,$start);

        $recordsTotal = $this->subcountyModel->countAll();
       
        $nextLength = (int)$length;
        $nextStart = ((int)$this->request->getVar("start")) + ((int)$length)+1;
        if($nextStart > $recordsTotal)
        {
            $nextLength = (int)$recordsTotal - (int)$start;
            $nextStart = (int)$start;
            $end = true;
        }


        $response = [
            'start' => $nextStart,
            'length' => $nextLength,
            'end' => $end,
            'total' => $recordsTotal,
            'data' => $data,
        ];
		
        return $this->respond($response);
    }

    public function getParishList()
    {
        $start = $this->request->getVar("start");
        $length = $this->request->getVar("length");
        $end = false;
        $data = $this->parishModel->findAll($length,$start);

        $recordsTotal = $this->parishModel->countAll();
       
        $nextLength = (int)$length;
        $nextStart = ((int)$this->request->getVar("start")) + ((int)$length)+1;
        if($nextStart > $recordsTotal)
        {
            $nextLength = (int)$recordsTotal - (int)$start;
            $nextStart = (int)$start;
            $end = true;
        }


        $response = [
            'start' => $nextStart,
            'length' => $nextLength,
            'end' => $end,
            'total' => $recordsTotal,
            'data' => $data,
        ];
		
        return $this->respond($response);
    }

    public function getVillageList()
    {
        $start = $this->request->getVar("start");
        $length = $this->request->getVar("length");
        $end = false;
        $data = $this->villageModel->findAll($length,$start);

        $recordsTotal = $this->villageModel->countAll();
       
        $nextLength = (int)$length;
        $nextStart = ((int)$this->request->getVar("start")) + ((int)$length)+1;
        if($nextStart > $recordsTotal)
        {
            $nextLength = (int)$recordsTotal - (int)$start;
            $nextStart = (int)$start;
            $end = true;
        }


        $response = [
            'start' => $nextStart,
            'length' => $nextLength,
            'end' => $end,
            'total' => $recordsTotal,
            'data' => $data,
        ];
		
        return $this->respond($response);
    }


    public function getVillageSearch(){
        $q = $this->request->getVar("q");
        $data = $this->villageModel->findWhereNameLike($q);
		
        return $this->respond($data);
    }
}